为什么程序在 while 循环中继续输入任何字符
Why does program continue in while loop for any character entered?
我退出while循环的条件有效,而留在循环中并输入另一个数字的条件有效。用户可以输入 Y 以留在循环中,但用户也可以输入任何单个字符并留在循环中。我不明白为什么当 Y 是比较时,它会将任何字符评估为 true?
int main()
{
string romNum;
char nextRoman;
int decimal = 0;
bool done = false;
bool invalCharacter = false;
int I_counter = 0;
int X_counter = 0;
int C_counter = 0;
int M_counter = 0;
int V_counter = 0;
int L_counter = 0;
int D_counter = 0;
while(done == false)
{
cout << "Enter a Roman Number and I will tell you its integer equivalent : " << endl;
cin >> romNum;
decimal = 0;
I_counter = 0;
X_counter = 0;
C_counter = 0;
M_counter = 0;
V_counter = 0;
L_counter = 0;
D_counter = 0;
invalCharacter = true;
for(int i = 0; i < romNum.length(); i++)
{
switch(romNum.at(i))
{
case 'M': decimal += 1000; M_counter += 1; break;
case 'D': decimal += 500; D_counter += 1; break;
case 'C': decimal += 100; C_counter += 1; break;
case 'L': decimal += 50; L_counter += 1; break;
case 'X': decimal += 10; X_counter += 1; break;
case 'V': decimal += 5; V_counter += 1; break;
case 'I': decimal += 1; I_counter += 1; break;
default : invalCharacter = false; break;
}
}
if(I_counter > 4 || X_counter > 4 || C_counter > 4 || M_counter > 4 || V_counter > 1 || L_counter > 1 || D_counter > 1 || invalCharacter == false)
{
cout << "Not a valid roman number. " << endl << endl;
}
else
{
cout << "The decimal value of the roman number is " << decimal << endl << endl;
}
cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl << endl;
cin >> nextRoman;
if(nextRoman == 'Y')
{
done = false;
}
else if(nextRoman == 'N')
{
done = true;
cout << "Thanks for roman numeraling with me. " << endl;
}
}
return 0;
}
试试这个(强制显式的"Y"或"N"响应):
nextRoman = '';
while (nextRoman != 'Y' && nextRoman != 'N') {
cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl << endl;
cin >> nextRoman;
if(nextRoman == 'Y')
{
done = false;
}
else if(nextRoman == 'N')
{
done = true;
cout << "Thanks for roman numeraling with me. " << endl;
}
else
{
cout << "What?" << endl;
}
}
在 if -else 部分添加一个条件:
if(nextRoman == 'Y')
{
done = false;
}
else if(nextRoman == 'N')
{
done = true;
cout << "Thanks for roman numeraling with me. " << endl;
}
else
{
done=false;
cout<<"see you next time"//or anything you want
}
现在会没事的。
cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl;
cin >> nextRoman;
if (nextRoman == 'Y')
{
done = false;
}
else if (nextRoman == 'N')
{
cout << "Thanks for roman numeraling with me. " << endl;
done = true; //I rather have the cout execute first then done = true;
}
}
在某些编译器中,您的代码可以正常工作,这可能是一个错误。
相关文章:
- 如何创建一个 while 循环,继续循环,直到在C++中输入'using streams'键?
- C++,程序输出无效输入,但继续通过循环
- 程序终止,而不是继续整数输入验证循环
- C++ 在 CIN 双精度时挂起,但如果输入整数,则继续
- QInputDialog & Ththreader(worker,show dialog,等待输入,继续)
- 如何在无效的数据类型输入后继续循环
- 为什么我不能继续使用 std::cin 向同一变量输入其他值?
- 使用ifstreamwhile循环,如何显示输入错误并在下一行继续
- 阻止 cin 继续接受输入
- 如何继续输入,直到用户按下空格键
- 如何只要用户愿意就继续读取输入
- 在文件结束后继续输入
- 等待输入C++时继续程序
- 即使输入文件不存在,也会继续创建输出文件?编辑****
- 为什么程序在 while 循环中继续输入任何字符
- 提示用户输入正数,并继续提示用户,直到他们输入正数 c++
- 程序工作,但当输入无效时,它会继续循环 c++
- visual C++.程序在输入后继续退出
- 程序不会在输入文件中继续读取它
- cin.有限制地忽略,继续丢弃新的输入