Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
卡在一个while循环中 我正在为我的C++类做作业。在这个作业中,我需要编写一个程序,从cin读取数字,然后求和,当使用while循环输入0时停止_C++_While Loop - Fatal编程技术网

卡在一个while循环中 我正在为我的C++类做作业。在这个作业中,我需要编写一个程序,从cin读取数字,然后求和,当使用while循环输入0时停止

卡在一个while循环中 我正在为我的C++类做作业。在这个作业中,我需要编写一个程序,从cin读取数字,然后求和,当使用while循环输入0时停止,c++,while-loop,C++,While Loop,我已经写了代码,并得到了我需要的结果。然而,我被困在一个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 >

我已经写了代码,并得到了我需要的结果。然而,我被困在一个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;
}
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

i
停留在
0
位置,在退出第一个循环后不会更改。不要在循环时使用另一个
,只需在循环结束后打印结果。

i
停留在
0
上,退出第一个循环后不会更改。不要使用另一个
while
循环,只需在之后打印结果。

删除
while(i==0)
。这总是正确的。

删除
,而(i==0)
。这永远是真的。

你的问题是你的第二个
while循环
你在循环
而(i==0)
但是
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;
}

请注意,计数包括终止的
0
,因此您可能希望报告
count-1

您的问题是第二个
while循环
您正在循环
,而(i==0)
但是
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;
}


请注意,计数包括终止的
0
,因此您可能希望报告
count-1

家庭作业标记已过时,因此请不要使用它。无论如何,基于这段代码,我有一个小建议:将最终的if-else更改为一组输出,并使用三元条件运算符根据比较结果输出“更大”或“更少”。如果运算符不符合您的喜好,请将If-else部分的主体更改为just
std::cout为什么有
while(i==0)
循环?你知道
i
是零,你不想循环。(同样,上面的循环增加了
i
太多次。)如果您陷入while循环,请检查打破循环的条件家庭作业标记已过时,因此请不要使用它。无论如何,基于这段代码,我有一个小建议:将最终的if-else更改为一组输出,并使用三元条件运算符根据比较结果输出“更大”或“更少”。如果运算符不符合您的喜好,请将If-else部分的主体更改为just
std::cout为什么有
while(i==0)
循环?你知道
i
是零,你不想循环。(此外,上面的循环会将
i
增加一倍。)如果您卡在while循环中,请检查中断循环的条件新代码中是否缺少导致其退出而不是打印结果的内容?新代码中是否缺少导致其退出而不是打印结果的内容?我删除了while循环,但现在当0为0时,程序退出输入。我发布了新代码。我不确定我是否错过了什么,或者只是做错了。看起来很棒!谢谢,我会试试的。谢谢你的帮助这样肯定更有意义。抱歉就行了。但我还是没有打印出来。我像您一样编写了代码,但它没有打印结果。一旦输入0,它将关闭程序窗口。更不用说我重新启动了程序,现在它工作正常。谢谢你的帮助^_^我删除了while循环,但现在当输入0时程序退出。我发布了新代码。我不确定我是否错过了什么,或者只是做错了。看起来很棒!谢谢,我会试试的。谢谢你的帮助这样肯定更有意义。抱歉就行了。但我还是没有打印出来。我像您一样编写了代码,但它没有打印结果。一旦输入0,它将关闭程序窗口。更不用说我重新启动了程序,现在它工作正常。谢谢你的帮助^_^
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.