c++进入死循环

标签 c++ visual-c++

<分区>

下面是我的代码:

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


...

为什么即使我没有开始向模式输入值,代码也会导致循环?

程序不应该等待用户输入“mode”吗?

最佳答案

将“名称”的类型更改为“字符串”

关于c++进入死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704925/

相关文章:

内联程序集错误 :- C2400: inline assembler syntax error in 'second operand' ; found 'newline' 中的编译器错误

c++ - 打印您使用参数定义的方法名称

c++ - C++ 中是否有标准的循环整数类?

c++ - 查找应用程序处理程序?

qt - 使用 Visual Studio 和 Qt 5.3 创建 QML 应用程序时退出时崩溃

c++ - 在 Windows 上使用 QT 运行 LibVLC 时禁用命令提示符

c++ - VS2010 - 如何解决 "XXX is a reserved class name"错误

c++ - constexpr vector push_back 或如何 constexpr 所有的东西

c++ - VisualStudio 中 C++ 项目中的预编译 header 未正确链接

c# - 在 Windows 系统上注销用户后立即让系统进入休眠状态