gpt4 book ai didi

c++ - 为什么这不会在接受输入后循环退出

转载 作者:太空宇宙 更新时间:2023-11-04 15:29:52 26 4
gpt4 key购买 nike

非常简单。我不明白为什么它在不接受 y 字符后不会退出循环。

关于 y char 和 n char,我尝试了 == 和 != 的不同变体

vector<int> v;
char ans;
do
{
cout << "Enter scores, enter number outside of 0-100 to stop:\n";
get_scores(v);
print_stats(v);
cout << "Do you want to try another set of scores? Y/N: \n";
cin >> ans;
} while (ans != ('n' || 'N'));

输入任何字符后,循环不断要求更多输入。注意:获取分数和打印统计数据功能按预期工作。

最佳答案

你在 while 条件下的比较不正确,你可能是故意的

while (ans != 'n' && ans != 'N');

('n' || 'N') 将被强制为真 (1),因此您将检查值为 1 的字符而不是 'n'/'N'

关于c++ - 为什么这不会在接受输入后循环退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187070/

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