C 循环停在首次迭代中

C++ Loop Stuck At First Iteration

本文关键字:迭代 循环      更新时间:2023-10-16

i获得了一个分配,以制作一个程序,使用户可以将12个月的每次降雨量输入到双打阵列中。

该计划应计算并显示一年的总降雨量,平均每月降雨以及最高和最低量的月份。

我的教授要求我简单地修改她为我们提供的骨骼。该程序汇编的问题很好,问题是,在第一个迭代中,它显示了第一个月,但停在那里。

不确定我做错了什么,有什么想法吗?

这是有关区域的代码。

void getMonthlyRainfalls(double rainfallsArr[], int size, int month){
 cout << "Please type the rainfalls occurring in the month " << (month+1) << ": " ;
 do{
  if (! cin){
     cin.clear();
     cin.ignore(1124, 'n');
  }
  for (int i = 0; i < size; i++)
      cin >> rainfallsArr[month];
  if (!cin || rainfallsArr[month] < 0)
      cout << "Please retype the rainfalls occurring in the month " << (month+1) << ": " ;
  } while (!cin || rainfallsArr[month] < 0);
 }

这是您分配的解决方案

#include <iostream>
using namespace std;
int main()
{
 double rain_fall[12] , maxi=0 , mini , sum=0; // maxi for the highest  
 //amount mini for the lowest
 for(int i=0; i<12; i++)
{
    cout<<"please enter the total amount of rainfall in the month number "
    <<i+1<< endl;
    cin>>rain_fall[i];
    if(i==0)
        mini=rain_fall[i];  // happened just once to give it an initial 
    value from the array
    if(maxi<rain_fall[i]) // the highest  amount
        maxi=rain_fall[i];
    else if (mini>rain_fall[i])
        mini=rain_fall[i];  // the lowest  amount
    sum+=rain_fall[i];
}
cout<<"the total amount in the year is : "<<sum<< endl;
cout<<"the average amount per month is : "<<sum/12<< endl;
cout<<"the highest  amount in the year is : "<<maxi<< endl;
cout<<"the lowest  amount in the year is : "<<mini<< endl;
return 0;
}

我在这里做什么
我做了一个快捷方式,以获得最高和最低的价值。从用户那里获取输入,这是代码的解释。

首先,我从用户那里获取第一个输入的输入,仅我给mini一个值一个值,如果我们为其分配了零,则必须从数组cuz的输入中给出一个值。月数大于零,但EX的最低数量为2,这将是一个错误,因为如果> 0> [值什么],那么给我最低金额的if语句将汇总最低的数量。每当它是错误的,所以迷你将保持零并显示为最低的值,但实际上我们在数组中没有零,最低值为2,因此,正如我们上面假设的具有包含金额的数组中存在的值

之后,我们对其进行if语句以与输入进行比较以节省其中的最大值,我们为其分配了零,因为任何东西都会大于零,因此(0&lt; [])),如果我们已经获得最大的价值

,然后给出错误

之后,我们在总和变量中互相添加输入以获取所有月份的总数

之后,在COUT语句中,以获取每月平均金额U分配12 [月数]

的总数

while(cin&amp;&amp; rainfallsarr [month]> 0);