卡在一段时间循环C中
stuck in a while loop c
我正在为我的C++课做作业。在这个作业中,我需要编写一个程序,从 cin 读取数字,然后对它们求和,当使用 while 循环输入 0 时停止。
我已经编写了代码并得到了我需要的结果。但是,我陷入了一个继续重新打印结果的循环中。谁能给我一些建议,在打印一次结果后结束这个循环?
这是我的代码:
int main ()
{
int i(1), sum(0);
int sum2(0);
const int max(10);
cout << "Enter numbers, one per line. Enter 0 to end:" << endl;
while (i!=0)
{
cin >> i;
sum += i; //add current value of i to sum
sum2 += 1;
}
while (i==0)
{
if (sum < 100) // If total is less than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is less than 100." << endl ;
else // Else total is greater than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is greater than 100." << endl ;
}
} //End of Int Main
希望结果没事。抱歉,我不确定如何在每行上发布数字。我还尝试将 while (i==0) 更改为 if 语句 if (i==0),但这会在输入 0 时关闭程序。有人有有用的建议吗?我将不胜感激。^_^
更新:抱歉,我忘了提到我还必须包含一个跟踪输入数的循环计数器、一个确定总值是小于还是大于 100 的注释,以及一个用于确定输入总数的语句。这就是最后 if else 语句的原因。我尝试去掉最后一个 while 语句,因为我知道这是无限循环的原因,但是当我这样做时它不会显示结果。我将代码更改为:
int main ()
{
int i(1), sum(0);
int sum2(0);
const int max(10);
cout << "Enter numbers, one per line. Enter 0 to end:" << endl;
while (i!=0)
{
cin >> i;
sum += i; //add current value of i to sum
sum2 += 1;
}
if (sum < 100) // If total is less than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is less than 100." << endl ;
else // Else total is greater than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is greater than 100." << endl ;
} //End of Int Main
我的输出应该是
Enter numbers, one per line. Enter 0 to end:
7
8
6
5
5
9
8
0
Thank you. The total was 48.
The total number of inputs read: 8
The total is less than 100.
>i
停留在0
,并且在退出第一个循环后永远不会更改。 不要使用另一个while
循环,只需在之后打印结果。
你的问题是你在循环while (i==0)
while loop
但i
循环内永远不会改变。
while (i==0) // Infinite loop, i is never change inside the block.
{
if (sum < 100) // If total is less than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is less than 100." << endl ;
else // Else total is greater than 100
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << sum2 << endl
<< "The total is greater than 100." << endl;
}
这里根本不需要循环结构。
编辑:你想要的是:
#include <iostream>
using namespace std;
int main(int argc, const char* argv[]) {
int i, sum, count;
cout << "Enter numbers, one per line. Enter 0 to end:" << endl;
while (i!=0) {
cin >> i;
sum += i;
count++;
}
cout << "Thank you. The total was " << sum << endl
<< "The total number of inputs reads: " << count << endl;
if (sum < 100)
cout << "The total is less than 100." << endl;
else
cout << "The total is greater than 100." << endl;
return 0;
}
演示:
$ ./a.out
Enter numbers, one per line. Enter 0 to end:
1000
100
10
0
Thank you. The total was 1110
The total number of inputs reads: 4
The total is greater than 100.
$ ./a.out
Enter numbers, one per line. Enter 0 to end:
1
2
3
0
Thank you. The total was 6
The total number of inputs reads: 4
The total is less than 100.
请注意,计数包括终止0
因此您可能希望改为报告count-1
。
删除while (i==0)
。 这永远都是事实。
相关文章:
- 在做一段时间内检查字符的无限循环
- ExtTextOut 文本的持续闪烁,在一段时间后,文本将恢复为默认字体
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- ::grpc::ServerReaderWriter 对象在另一个线程中一段时间后无法调用
- 一段时间后 CPU 使用率高
- 我的开关菜单在一段时间循环中出现问题
- 执行/在循环中停留一段时间,例如 1 分钟
- 如何在嵌套的 for 循环中使用“继续一段时间”循环
- 一段时间循环中的奇怪错误(C++)
- 在CIN为真后,使用一段时间循环,然后执行循环
- 在一段时间循环中使用std::cout的奇怪行为
- GDI 窗口中的游戏循环/一段时间后不刷新并崩溃
- 奇怪的cout行为在一段时间内循环C++
- 在一段时间循环比较中实现 getline
- 在一段时间循环中
- 如何在一段时间循环中使用时钟
- 如何在一段时间循环中使用按键切换布尔值
- 你如何做一个 for 循环作为一段时间的条件
- 为什么刹车后做一段时间的循环会更好
- 卡在一段时间循环C中