C 循环停在首次迭代中
C++ Loop Stuck At First Iteration
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);
相关文章:
- 擦除while循环中迭代的元素
- 对于set上的循环-获取next元素迭代器
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 将多个 for 循环组合成单个迭代器
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 使用一个 for 循环如何迭代和测试迭代器值?
- 循环挂起迭代的 std::擦除 on std::list
- 使用基于范围的循环迭代矢量时出现问题
- 整个循环花费的时间比其迭代总和要长
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- C++ Code 在 for 循环期间不会累积变量中的总和,仅提供最终迭代值
- Visual Studio 2013 C 的迭代循环
- 矢量迭代循环引发错误
- C++:如何将数据从迭代循环输出到列中
- 迭代循环重复的次数比我预期的要多
- c++ 11线程在迭代循环时间
- 迭代循环与索引循环