卡在一个循环c

时间:2013-01-25 23:15:55

标签: c++ while-loop

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

我已经编写了代码并得到了我需要的结果。但是我陷入了一个继续重新打印结果的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

希望这一切顺利。对不起,我不知道如何在每行发布数字。我也尝试将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.

3 个答案:

答案 0 :(得分:2)

i一直停留在0,并且在退出第一个循环后永远不会改变。而不是使用另一个while循环,只需打印结果。

答案 1 :(得分:1)

删除while (i==0)。这将永远是真的。

答案 2 :(得分:1)

您的问题是您的第二个while loop循环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;
}

这里根本不需要循环结构。

修改:您想要的是:

#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