【问题标题】:stuck in a while loop c卡在一个while循环c
【发布时间】:2013-01-25 23:15:54
【问题描述】:

我正在为我的 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.

【问题讨论】:

  • 作业标签已废弃,请勿使用。无论如何,我有一个仅基于此代码的小建议:将最终的 if-else 更改为一组输出,并使用三元条件运算符根据比较输出“更大”或“更少”。如果操作符不符合您的要求,请将 if-else 部分的主体更改为 std::cout &lt;&lt; "greater";std::cout &lt;&lt; "less"; 并将其他输出部分放在外面。这是为了消除不必要的代码重复。
  • 为什么会有while (i==0) 循环?你知道 i 是零,你不想循环。 (另外,上面的循环将i 增加了太多次。)
  • 如果你被困在一个while循环中,检查打破循环的条件

标签: c++ while-loop


【解决方案1】:

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

【讨论】:

  • 我在新代码中是否缺少某些内容导致它退出而不是打印结果?
【解决方案2】:

您的问题是您的第二个 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

【讨论】:

  • 我删除了while循环,但现在程序在输入0时退出。我发布到新代码。我不确定我是遗漏了什么还是只是做错了。
  • 看起来很棒!谢谢你,我会试一试。感谢您的帮助! ^_^ 这样肯定更有意义。
  • 对不起就好。不过,它仍然没有为我打印出来。我像你一样写了代码,但它没有打印结果。
  • 一旦输入 0 就关闭程序窗口。
  • 没关系,我重新启动了程序,现在它可以完美运行了。谢谢您的帮助! ^_^
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多