I looked at this the other day, in fact debugged the whole thing, but deleted my code, as that thread was marked as solved.
Anyway, in the code from the opening post of this thread, at line 19:
That's an array with one element. It can hold the data for January - at line 30, this is ok:
But that's it. Full up now. So what happens at line 35?
Well, the value is stored in an adjacent memory location,
outside the array, and some other data gets corrupted. And that is a serious problem.
So make that array at least
or maybe
float month[12];
to hold an entire year's worth of data.
In function
getMonthly()
starting at line 66, there is a for loop.
inside the loop a new array
float rainfall[30] ;
is declared. Now its a bit pointless declaring it there, as only one element is ever used before it gets destroyed and a fresh version created in the next iteration of the loop. But it doesn't do any harm, does it?
Well, actually it does a lot of harm. Note it is declared as having 30 elements. That's good enough for some months. But what happens in January, and get to line 76
cin >> rainfall[day];
on the 31st day. Where does the input value go? Well, you can be pretty sure it's somewhere bad. Some other value will get corrupted, with unpredictable effects.
If you
really need to store each individual value, then you need a different solution. But for now I'd say just simplify the function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
float getMonthly( const int days )
{
float rain_total = 0.0f;
float rainfall;
for ( int day = 0; day < days; ++day )
{
cout << "\nPlease enter the rainfall for day " << 1+day << ": ";
cin >> rainfall;
rain_total += rainfall;
}
return rain_total;
}
|
Those are the big problems.
There's still the minor one of line 38
cout<<Year_Rain;
which is not needed - as I pointed out previously.
http://www.cplusplus.com/forum/beginner/118142/#msg644350