gpt4 book ai didi

菜单中的 C++ 循环

转载 作者:太空狗 更新时间:2023-10-29 19:39:35 41 4
gpt4 key购买 nike

在我的代码中存在一个问题,即即使我输入“Q”或“q”,程序也会继续循环菜单。这里出了什么问题?这是代码:

{
char selection;
do {
cout << "Add a county election file A" << endl;
cout << "Show election totals on screen P" << endl;
cout << "Search for county results S" << endl;
cout << "Exit the program Q" << endl;
cout << "Please enter your choice: ";
cin >> selection;
} while ((selection != 'Q' || selection != 'q'));
return 0;
}

最佳答案

您想在测试中使用 And (&&) 运算符,而不是 Or (||) 运算符。否则,selection != 'Q'selection != 'q' 之一将始终为真,您的循环将永远不会退出。

关于菜单中的 C++ 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13110185/

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