gpt4 book ai didi

c++ - 在 do/while 循环中跳过 Cin 输入

转载 作者:行者123 更新时间:2023-11-28 07:34:32 30 4
gpt4 key购买 nike

cin >> subchoice;
switch(subchoice)
{
case 1:
gold = gold - 5;
cout << "Now you only have " << gold << " gold.\n";
cout << "blah blah";
health = 100;
break;
case 2:
cout << "blah blah"
<< "1 - Head Back in and Buy a Bed\n"
<< "2 - Find a Clothier Shop\n"
<< "3 - Find a Blacksmith's\n";
cin >> subsubchoice;
switch(subsubchoice)
{
case 1:
gold = gold - 5;
cout << "Now you only have " << gold << " gold.\n";
cout << "blah blah"
health = 100;
break;
case 2:
menuchoice = subsubchoice;
break;
case 3:
menuchoice = subsubchoice;
break;
}
}

break;
case 2:

}




} while (!boolchoice);

嘿,我这里有这段代码。每当我运行它时,它总是会跳过 subsubchoice 的 cin 输入。它应该做的是获取该输入,然后让您进入旅馆或将该选择应用于 menuchoice 并在 do-while 循环的开头重新启动您。我不知道为什么它会跳过输入然后关闭程序。感谢您的帮助。

最佳答案

也许你需要在 cin >> subsubchoice; 之前添加 cin.ignore()

关于c++ - 在 do/while 循环中跳过 Cin 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17006038/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com