【发布时间】:2023-03-25 10:34:01
【问题描述】:
我的 if 语句一直运行,就好像条件已经满足,即使它们没有满足。我尝试过移动一些代码,甚至以不同的方式重写 if 语句,但它并没有改变结果。有谁知道我做错了什么?
#include <iostream>
#include <string>
using namespace std;
double num, num2, num3, num4, num5, num6, sum;
char input;
bool continueBool = true;
string bob;
void math()
{
cout << "Please enter your first number" << endl;
cin >> num;
cout << "Please enter your second number?" << endl;
cin >> num2;
cout << "Please enter your third number?" << endl;
cin >> num3;
cout << "Please enter your fourth number" << endl;
cin >> num4;
cout << "Please enter your fith number?" << endl;
cin >> num5;
cout << "Please enter your sixth number?" << endl;
cin >> num6;
sum = num + num2 + num3 + num4 + num5 + num6;
}
void ifStatement() {
if (bob == "no", "No", "NO", "nO") {
continueBool = false;
cout << "Good bye!" << endl;
}
}
int main()
{
while (continueBool = true) {
math();
cout << "The sum of your numbers is: " << sum << endl;
cout << "Would you like to add any more numbers together?" << endl;
cin >> bob;
ifStatement();
return 0;
}
}
【问题讨论】:
-
警告:
while (continueBool = true)-->while (continueBool)..... 比while (continueBool == false == false)好。底线:检查布尔值==true是否毫无意义,只需检查布尔值本身。 -
总是
return从循环内部生成循环:not-a-loop。虽然当您遇到无限循环错误时,也许这不是一件坏事。
标签: c++