【发布时间】:2015-02-01 21:27:17
【问题描述】:
我编辑了这个问题,以准确地表达我的要求,并清楚地表明我的错误。这个问题已经回答了。
为什么我会陷入死循环?我尝试了一些不同的技术,例如在 if 语句中引入一个 break 语句,甚至只是在其中抛出一个 break 语句。但是我仍然陷入困境。
while (recalculate = 1){
cout << "\nEnter in the radius of the sphere: ";
cin >> radius;
cout << "\nEnter in the weight of the sphere: ";
cin >> weight;
cout << "\n";
if (bForce > weight)
{
cout << "\nEgads, it floats!\n";
}
else {
cout << "\nIt sunk...\n";
}
cout << "\nRecalculate? (1 = yes, 0 = exit)\n";
cin >> recalculate;
// See what recalculate really is.
cout << "\n" << recalculate;
}
【问题讨论】:
-
因为你正在分配变量
recalculate = 1更改为recalculate == 1 -
打开你的编译器警告!!!
-
这已经被回答了。我编辑了这个问题,使它不是一团糟。我确实有编译器警告,但这是一个逻辑错误。 while循环继续将我的变量分配给1,而不是比较它,并且无论如何都继续一遍又一遍地循环。
标签: c++ loops if-statement while-loop