gpt4 book ai didi

c++ - 嵌套循环最有可能因为不正确的 bool 运算符而重复自身 C++

转载 作者:行者123 更新时间:2023-11-28 00:20:52 29 4
gpt4 key购买 nike

我的问题是,如果我输入除 1 以外的任何内容,第二个 do while 循环会重复自身。如果我在询问我是否确定它退出循环时输入 1,但任何其他数字只会使控制台窗口继续重复它的问题而不给我回答的机会。我使用 C++ 的时间还不到一周,但我认为这些行就是问题所在。

} while ( x != 2 || 1);
} while (x != 1);

#include <iostream>
#include <string>
using namespace std;
struct adress{
string name;
string adress;
int phonenumber;
};
adress fillform()
{
int x;
adress form;
do{
cout << "What's your name" << endl;
cin >> form.name;
cout << "What's your adress" << endl;
cin >> form.adress;
cout << "What's your phone number" << endl;
cin >> form.phonenumber;
cout << "Your name is " << form.name << endl;
cout << "Your adress is " << form.adress << endl;
cout << "Your phone number is " << form.phonenumber << endl;
do{
cout << "Is this information correct?\n1. Yes 2. No" << endl;
cin >> x;
switch ( x )
{
case 1:
cout << "Okay" << endl;
break;
case 2:
cout << "Try again" << endl;
default:
cout << "Invalid answer" << endl;
}
} while ( x != 2 || 1);
} while (x != 1);
return form;
}
int main()
{
fillform();
}

最佳答案

x != 2 || 1 代表 (x != 2) || 1 始终为真。

你需要 x != 2 && x != 1

关于c++ - 嵌套循环最有可能因为不正确的 bool 运算符而重复自身 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590979/

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