Хорошо, единственная проблема с этой программой находится в конце, когда я пытаюсь решить, какие месячные значения осадков были самыми низкими или самыми высокими. Мой вывод для любого из них отлично работает. UNLESS «Jan» (январь) является либо самым высоким, либо самым низким. Затем либо он останется пустым. Все остальные варианты отлично работают. Любая идея о том, почему это происходит? Является ли индекс моего массива где-то? Заранее благодарю за любой совет.Выходные данные отсутствуют «Январь»
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
//declare and initialize variable and arrays
string year[] = {"Jan", "Feb", "Mar", "April", "May", "June",
"July", "August", "Sept", "Oct", "Nov", "Dec"};
const int totalMonths = 12;
double month[totalMonths];
double average;
double total = 0;
cout << "This program will calculate the total annual rainfall,\n";
cout << "calculate the monthly average rainfall, and display\n";
cout << "which month had the lowest amount of rainfall and which\n";
cout << "month had the highest amount of rainfall. Please enter the\n";
cout << "rainfall data in inches." << endl;
cout << "----------------------------------------------------------" << endl;
// prompt user for input, keep a total sum of data entered
for(int i = 0; i < 12; i++)
{
cout << "Enter total rainfall for " << year[i] << endl;
cin >> month[i];
total += month[i];
while(month[i] < 0)
{
cout << "Rainfall cannot be negative!\n";
cout << "Please re-enter positive numbers" << endl;
cin >> month[i];
}
}
//average the total rainfall
average = total/totalMonths;
cout << setprecision(1) << fixed;
cout << "Total Annual rainfall is: " << total << endl;
cout << "The average rainfall per month is: " << average << endl;
//determine which month had the lowest and highest amount of rainfall
double highest = 0;
string highMonth;
highest = month[0];
for(int count = 0; count < totalMonths; count++)
{
if(month[count] > highest)
{
highest = month[count];
highMonth = year[count];
}
}
double lowest = 0;
string lowMonth;
lowest = month[0];
for(int count = 0; count < totalMonths; count++)
{
if(month[count] < lowest)
{
lowest = month[count];
lowMonth = year[count];
}
}
cout << "The month with the highest rainfall is: " << highMonth << endl;
cout << "The month with the lowest rainfall is: " << lowMonth << endl;
return 0;
}
Пример вывода:
This program will calculate the total annual rainfall,
calculate the monthly average rainfall, and display
which month had the lowest amount of rainfall and which
month had the highest amount of rainfall. Please enter the
rainfall data in inches.
----------------------------------------------------------
Enter total rainfall for Jan
1
Enter total rainfall for Feb
2
Enter total rainfall for Mar
3
Enter total rainfall for April
4
Enter total rainfall for May
5
Enter total rainfall for June
6
Enter total rainfall for July
7
Enter total rainfall for August
89
Enter total rainfall for Sept
12
Enter total rainfall for Oct
13
Enter total rainfall for Nov
14
Enter total rainfall for Dec
15
Total Annual rainfall is: 171.0
The average rainfall per month is: 14.2
The month with the highest rainfall is: August
The month with the lowest rainfall is: <-------- //BLANK!
Благодарим вас за помощь, а также за выявление ошибки проверки ввода! Я сделаю все возможное, чтобы исправить это – Gmenfan83