为什么开关在条件时无法执行
Why switch won't execute while condition
谁能给我解释一下为什么这段代码不会执行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;
}
相关文章:
- 无论条件是否为true,if总是在c++中执行
- 为什么我的最后一个 ELSE 条件无法正确执行
- 当我的 if 条件计算结果为 false 时,我的 else 块将不会执行
- C++ 在条件未按预期工作时执行
- 子线程中的条件变量等待停止主线程中的执行
- Craps游戏在循环不满足退出条件时执行
- 线程功能之一不执行可能的比赛条件
- 机器码层上的虚函数和条件执行的区别
- 为什么在执行条件变量通知之前我们需要一个空的 std::lock_guard?
- for 循环中的条件未执行
- 字符在循环条件下执行
- 在什么条件下,数据库在从 cpp 执行选择查询时不会关闭游标
- 如果参数条件为 true,则阻止执行返回函数
- 使用 gtest EXPECT_CALL 时竞争条件段错误,而另一个期望是执行相同的方法
- c while()..执行代码行的条件
- 同时执行 if 和 else 条件的逻辑是什么
- 为什么我的else条件从未执行
- 尽管有停止条件,无限递归调用仍会执行,因为参数不会前进
- 如何在QtCreator中基于SqlQuery结果执行条件语句
- C++三元运算符执行条件