无法确定如何放置循环

时间:2013-11-28 13:56:39

标签: c++

我无法弄明白......

当金额== 0时,我希望程序退出,但如果我不完成整个过程,我想再次询问初始问题。当我输入0时,它正在崩溃。我不能使用if语句,只能使用循环。

main()
{
      int amount = 1;
      while(amount != 0)
      {
      cout <<"How many numbers do you wish to average? ";
      cin >> amount;
      int avg = 0, sum = 0;

            for (int lim = 0; lim < amount; lim++)
            {             
                cout <<"Enter a value (If multiple, one at a time)"<< endl;
                sum = sumNums(sum);
            }

            avg = sum / amount;
            displaySum (sum, avg);

      }
}

int sumNums (int sum)
{
    int number = 0;
    cin >> number;
    sum = sum + number;
    return sum;   
}

void displaySum (int sum, int avg)
{
     cout <<"The sum is "<< sum <<" and the average is "<< avg << endl;     
}

6 个答案:

答案 0 :(得分:1)

avg = sum / amount;

当行数== 0时,此行崩溃,您需要使用if语句保护它。

答案 1 :(得分:0)

你除以零 这会导致程序崩溃。

答案 2 :(得分:0)

if (amount == 0)
    break;

在读取量之后放置它,它将强制循环结束,并避免除以0。

没有if,再次获得金额后:

while (amount == 0)
    break;

编辑:

等待这个不起作用,你只能在测试循环之外,你应该使用return 0来退出程序。

答案 3 :(得分:0)

这不太难。

#include <iostream>
using namespace std;

int sumNums(int sum);
void displaySum(int sum, int avg);

main()
{
      int amount = 1;
      for (;;)
      {
      cout <<"How many numbers do you wish to average? ";
      cin >> amount;
    **while (amount == 0) return 0;** // use this to halt execution

    **while (amount != 0){**
      int avg = 0, sum = 0;

            for (int lim = 0; lim < amount; lim++)
            {             
                cout <<"Enter a value (If multiple, one at a time)"<< endl;
                sum = sumNums(sum);
            }

            avg = sum / amount;
            displaySum (sum, avg);
        amount=0;
    }
      }
}

int sumNums (int sum)
{
    int number = 0;
    cin >> number;
    sum = sum + number;
    return sum;   
}

void displaySum (int sum, int avg)
{
     cout <<"The sum is "<< sum <<" and the average is "<< avg << endl;     
}

答案 4 :(得分:0)

您可以执行以下操作,但您可能需要添加一个提示,要求用户添加高于零的金额。

int amount = 1;
while(true){
  do{
     while(amount==0) count << "ERROR: Enter a number greater than 0" << endl;
     cout <<"How many numbers do you wish to average?";
     cin >> amount;
  }while(amount == 0);
  int avg = 0, sum = 0;

        for (int lim = 0; lim < amount; lim++)
        {    
            do{
               cout <<"Enter value" << lim+1 << "/" << amount << endl;
               sum = sumNums(sum);
            }while(sum==0);
        }

        avg = sum / amount;
        displaySum (sum, avg);

  }

答案 5 :(得分:0)

您可以像这样编写代码:

#include <iostream>
using namespace std;
int sumNums (int);
void displaySum (int, int);

int main()
{
  int amount = 1;
  cout <<"How many numbers do you wish to average? ";
  cin>>amount;
  while(amount != 0)
  {

  int avg = 0, sum = 0;

        for (int lim = 0; lim < amount; lim++)
        {             
            cout <<"Enter a value (If multiple, one at a time)"<< endl;
            sum = sumNums(sum);
        }

        avg = sum / amount;
        displaySum (sum, avg);
        cout <<"How many numbers do you wish to average? ";
        cin >> amount;

  }
}

int sumNums (int sum)
{
    int number = 0;
    cin >> number;
    sum = sum + number;
    return sum;   
     }

void displaySum (int sum, int avg)
{
     cout <<"The sum is "<< sum <<" and the average is "<< avg << endl;     
}