为什么我陷入了循环
Why am I Stuck in a Loop?
我编辑了这个问题,以精确地表达我所问的内容,并清楚地表明我的错误。这个问题已经得到了回答。
为什么我陷入了无休止的循环?我尝试了几种不同的技术,例如在 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;
}
while(recalculate=1)
总是被计算为true,因为1被分配给recalculate
,任何不同于零的数值都被隐式转换为布尔true
。要测试相等性,请使用==
,即
while(recalculate == 1)
问题是=
是一个赋值,而不是一个比较:
if (recalculate = 0) {
上面将recalculate
设置为零,然后将其计算为布尔表达式。如果为零,则其计算结果始终为 false,因此永远不会执行if
的主体。
编写比较的正确方法是==
:
if (recalculate == 0) {
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 为什么在这个代码结束循环中没有得到结束
- 为什么我无法更改"set<set>"循环中的值<int>
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 为什么我不能在 while 循环中创建线程?
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么在递归中使用循环会产生意想不到的结果?
- 为什么在 while 循环中返回表达式不起作用
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 而循环:简单的除法程序输出零,不明白为什么
- 为什么我在尝试在单向链表中打印元素时会出现这个永无止境的循环
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么循环不会停止?C++
- 我的CLL程序在c++中不会停止循环!为什么
- (c++) For循环-为什么它这样做