gpt4 book ai didi

C++:do while 循环有问题

转载 作者:行者123 更新时间:2023-11-27 22:48:25 28 4
gpt4 key购买 nike

do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' || 'C');

我将 account_type 设置为 char,问题是每次我运行程序并输入 S 或 C 时,循环不断重复。谁能帮我知道为什么会这样?

最佳答案

在 bool 运算中使用时,C++ 中的所有非零值都计算为 true。所以 account_type != 'S' || 'C'account_type 相同!= 'S' ||真。这意味着您的循环永远不会退出。

您需要做的是执行这两项检查

do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' && account_type != 'C');

关于C++:do while 循环有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591855/

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