I'm making a simple program that is supposed to log rainfall data for a year. The program is supposed to display two months with the most and least amount of rainfall. I'm having a hard time understanding why my program can find the highest month but not the lowest when it runs. I show the whole code because I'm not sure if the issue is in the for loop or at the top. Suggestions are much appreciated!
At lines 12 and 13, the rain array hasn't been filled with valid values yet, so they have some uninitialized garbage value.
lines 14 and 15 - these variables don't get initialized. So if the lowest or highest value happens to be in the first element, these variables won't get set, since you only set them in the if statement if the later elements are less than or greater than the first.
I moved lines 12 and 13 to after the for loop that gathers the rainfall data, and initialized the strings at line 14 and 15 to months[0] and it seems to work ok.
line 9 - I'd make this a double since you're summing up double values in the array.
double least;// = rain [0];//Error prone, rain[0] is not initialized
double most;// = rain [0];//Error prone, rain[0] is not initialized
Count from 0:
1 2
for (int t = 1; t<12; t++) // find month with least rainfall
for (int q = 1; q<12; q++) // find month with most rainfall
Optional:
Prevent program crash in character entry:
1 2 3 4
cout << "Enter rain for month of " <<months[i] <<" ";
cin >> rain [i];
cin.clear(); //Unnecessary, but removes crash from alpha input
cin.ignore(255,'\n'); //Tries to clear cin buffer with a 255 character extraction
Prompt the user for exit, instead of hang-like behavior:
1 2
cout<<"\n\nPress <enter> to exit: ";//Inform the user of exit:
cin.get();
!Spoiler!:
A working example: http://pastie.org/private/pb3kadbyx06xqwowkg5fdw
The problem is the the least and leastmonth variables in lines 36 & 37. I tried AbstractionAnon's suggestion and the least variable was fixed (Thanks for that! :)). for some reason though, the leastmonth variable still displays blank when I run the program.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main ()
{
double total = 0.0;
double rain [12]; // holds rainfall data
string months [12] = {"January", "February", "March", "April","May", "June", "July","August","September","October","November","December"};
string leastmonth = months[0]; //initialize to first month
string mostmonth = months[0]; //initialize to first month
// enter rainfall for each month
for (int i=0; i<12; i++)
{
cout << "Enter rain for month of " <<months[i] <<" ";
cin >> rain [i];
total += rain[i]; //increment total
cout <<endl;
}
// average monthly rainfall
double avgrainmonth = total/12;
// find month with least rainfall
double least = rain [0];//initialize to first month's rainfall
for (int t = 1; t<12; t++)
{
if (rain [t] < least)
{
least = rain [t];
leastmonth = months[t];
}
}
// find month with most rainfall
double most = rain [0];//initialize to first month's rainfall
for (int q = 1; q<12; q++)
{
if (rain[q] > most)
{
most = rain[q];
mostmonth = months[q];
}
}
cout <<"The month with the least rainfall was " <<leastmonth <<" with "<<least <<" rainfall" <<endl;
cout <<"The month with the most rainfall was " <<mostmonth <<" with "<<most <<" rainfall" <<endl;
cout <<"average rainfall is " <<avgrainmonth;
return 0;
}
Enter rain for month of January
Enter rain for month of February
Enter rain for month of March
Enter rain for month of April
Enter rain for month of May
Enter rain for month of June
Enter rain for month of July
Enter rain for month of August
Enter rain for month of September
Enter rain for month of October
Enter rain for month of November
Enter rain for month of December
The month with the least rainfall was January with 1.5 rainfall
The month with the most rainfall was December with 12.5 rainfall
average rainfall is 6.90833
Thanks for your help everyone, I got it working. Especially wildblue, your explanation about the placement and initialization of those variables really cleared up the issue for me