gpt4 book ai didi

c++ - 无法弄清楚如何为用户提供启动、停止和重新启动程序的所有选项?

转载 作者:行者123 更新时间:2023-11-30 04:53:03 25 4
gpt4 key购买 nike

好的,所以我正在尝试构建这个随机数柜员,它基本上会告诉用户他们输入的数字是小于、大于还是等于 50,并为他们提供启动、停止和重新启动“随机数出纳员”这是代码:

    #include <iostream>


using namespace std;

main() {
cin >> boolalpha;

int invalid_answer {0};
const int const_num {50};
int random_num {};
char answer {};
int keep_going {};

while (keep_going == 0) {
while (invalid_answer == 0) {

//=======================================================================================================================================

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "Want to try again? Type \"Y\" or \"N\"";
cin >> answer;

//=======================================================================================================================================

if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}

//=======================================================================================================================================

while(answer == 'Y') {

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "\nWant to try again? Type \"Y\" or \"N\"";
cin >> answer;
}

//=======================================================================================================================================

if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

//=======================================================================================================================================

while (invalid_answer == 1) {
cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
cin >> answer;

if (answer == 'Y') {
invalid_answer = 0;
}
else if (answer == 'N') {
cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}
}
}
}

}

每当我说“N”表示“否”时,我不想重做随机数检查器,它不会将 keep_going 更改为 1,它只会移动到它下面的另一个 if 或 while 语句之一。因此,当您输入“N”时,它只会输出 "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": ""I'm sorry what? Please note that answers are case sensitive. Answer again: "

最佳答案

问题出在这段代码上:

   if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

answer'N'时,answer != 'Y'true并且 invalid_answer 设置为 1(由于短路评估,甚至不评估逻辑 OR 的 rhs - 参见下面的引用)。
所以执行会进入while

while (invalid_answer == 1)

并将打印语句。

您可以通过以下方式更正此问题:

if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
invalid_answer = 0;
}
else { //for all other inputs
invalid_answer = 1;
}

Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands

另请注意,main 的类型应为 int

关于c++ - 无法弄清楚如何为用户提供启动、停止和重新启动程序的所有选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54019204/

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