下面是我的代码:
char name;
bool isValid = true;
int mode;
cout << "Enter name:" << endl;
cin >> name;
do
{
cout << "Choose a mode" << endl;
cin >> mode;
switch (mode)
{
case 1:
iniCharacter (name, 110, 100, 100);
break;
case 2:
iniCharacter (name, 100, 110, 100);
break;
case 3:
iniCharacter (name, 100, 100, 110);
break;
default:
isValid = false;
cout << "Invalid mode, ";
break;
}
}while (!isValid);
但是当我运行上面的代码时,输出如下:
[output]Please enter name:
[input] test
[output] Invalid mode
[output] Invalid mode
[output] Invalid mode
[output] Invalid mode
[output] Invalid mode
...
为什么即使我没有开始向模式输入值,代码也会导致循环?
程序不应该等待用户输入“模式”吗?