为什么开关在条件时无法执行

Why switch won't execute while condition

本文关键字:执行 条件 开关 为什么      更新时间:2023-10-16

谁能给我解释一下为什么这段代码不会执行while条件?我只是想知道为什么代码以这种方式表现,或者是否有其他方法使其工作。由于

更新! !

嗨,顺便说一下,这是代码,我不太熟悉c++,所以我不确定程序是否跳过了开关上的while条件。由于

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAX 100
using namespace std;
int main()
{
void remove(char[]);
void add(char[], char);
char cstring[MAX];
char letter;
int ecount;
std::string str;
char selection;
cout << "Enter String: ";
cin.getline(cstring, MAX);
cout << "Size of String: " << strlen(cstring) << endl;
bool gameOn = true;
while (gameOn != false){
cout<<"n Menu";
cout<<"n========";
cout<<"n A - Append";
cout<<"n X - Exit";
cout<<"n Enter selection: ";
cin>>selection;
switch(selection)
{
case 'A' :
case 'a' :
    cout << " Add Element: ";
    while (letter!='n')
    {
    letter = cin.get();
    add(cstring, letter);
    }
    cout << "n Output String: " << cstring;
    ecount = strlen(cstring) - 1;
    cout << " Size of String: " << ecount<< endl;
    break;
case 'X' :
case 'x' :{cout<<"n To exit the menu";}
    break;
    default : cout<<"n !!Invalid selection!!"<< endl;
    }
}
cout<<"n";
return 0;
}

char * add( char *cstring, char c )
{
   int letter = strlen( cstring );
   if ( letter < MAX - 1 )
   {
      cstring[letter] = c;
      cstring[letter + 1] = '';
   }
   return ( cstring );
}

这是我的胡乱猜测。我认为你必须初始化letter

通过使用以下代码检查它。是的,调试您传递给switch的selection的值。

switch(selection)
{
case 'A' :
case 'a' :
    cout << "Add Element: ";
    letter = cin.get(); // this will initialize the char letter;
    while (letter != 'n') // or use ascii value of new line char in while condition 
    {
      add(cstring, letter); // first add it to string
      letter = cin.get(); // then get next letter  
    }
   cout << "String: " << cstring;
   ecount = strlen(cstring) - 1; // ecount must be a int
   cout << "Size of String: " << ecount<< endl;
break;

希望对你有帮助

问题在于你正在阅读的输入,当你在

中读取一个字符时
cin>>selection;

它将读取一个字符并将其放入选择中,当你按enter键时,它将被存储在letter中,并且你的while循环将永远不会执行。

查看下面的代码,它工作正常;

#include<iostream>
#include <stdio.h>
#include <string.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAX 100
using namespace std;
int main()
{
    void remove(char[]);
    void add(char[], char);
    char cstring[MAX];
    char letter;
    int ecount;
    std::string str;
    char selection;
    cout << "Enter String: ";
    cin.getline(cstring, MAX);
    cout << "Size of String: " << strlen(cstring) << endl;
    bool gameOn = true;
    while (gameOn != false){
        cout<<"n Menu";
        cout<<"n========";
        cout<<"n A - Append";
        cout<<"n X - Exit";
       cout<<"n Enter selection: ";

       cin>>selection;
       cout<<"n selection="<<selection;
       switch(selection)
       {
        case 'A' :
        case 'a' :
              selection=cin.get();
              cout << " Add Element: ";
              while (letter!='n')
               {
                letter = cin.get();
                 add(cstring, letter);
               } 
               cout << "n Output String: " << cstring;
               ecount = strlen(cstring) - 1;
               cout << " Size of String: " << ecount<< endl;
         break;
      case 'X' :
      case 'x' :cout<<"n To exit the menu";
                gameOn=false;
      break;
      default : cout<<"n !!Invalid selection!!"<< endl;
      } 
    }
    cout<<"n";
    return 0;
   }