Rainfall Statistics/Array problem

Hey guys, I know you've probably seen questions regarding this program quite a few times on the forum, but I couldn't find one pertaining to the specific problem I had. I've done pretty much the entire program, but I'm having just one little hang up here.

The program runs exactly as it's expected to: the user enters rainfall statistics for each month, and the program outputs the total yearly rainfall, the average rainfall, and the months with the highest and lowest rainfall. It all runs great except for one thing. Whenever January is entered as a month with the highest or lowest rainfall, the program doesn't output the month name, and just leaves the space blank. The rainfall amount is still displayed fine. No other months have this problem, and it still counts January's rainfall towards the total count and the average. I know it has to do with something within the array, but I've been looking at it for a few days now, and I just can't figure it out myself.

I'm a beginner to C++ and new to these forums, so I'd appreciate some tips or hints as to what's wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
using namespace std;

int main () 
{	
	double avgRainfall = 0;
	double totalRainfall = 0;
	double lowestRainfall;
	double highestRainfall;
	string lowestMonth;
	string highestMonth;
	string monthNames[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	double monthlyTotals[12];
	char repeat;

	do
	{
		cout << "Enter the rainfall for each month, in inches: " << endl;


		//Input and positive number validation
		for (int i = 0; i < 12; i++)
		{
			cout << monthNames[i] << ": ";
			cin >> monthlyTotals[i];

			while (monthlyTotals[i] < 0)
			{
				cout << "Please enter a positive number for " << monthNames[i] << endl;
				cin >> monthlyTotals[i];
			}
		}	
		
	
		//Sum
		for (int i = 0; i < 12; i++)
		{
			totalRainfall = totalRainfall + monthlyTotals[i];
		}

		//Average
		avgRainfall = totalRainfall / 12;

		//Highest and Lowest months

		highestRainfall = monthlyTotals[0];
		lowestRainfall = monthlyTotals[0];

		for (int i = 0; i < 12; i++)
		{
			if (monthlyTotals[i] > highestRainfall)
			{
				highestRainfall = monthlyTotals[i];
				highestMonth = monthNames[i];
			}

			if (monthlyTotals[i] < lowestRainfall)
			{
				lowestRainfall = monthlyTotals[i];
				lowestMonth = monthNames[i];
			}
		}	
	
		//Output
		cout << "The total rainfall throughout the year was " << totalRainfall << " inches." << endl;
		cout << "The average rainfall for the entire year is " << avgRainfall << " inches." << endl;
		cout << "The month with the lowest rainfall is " << lowestMonth << ", with a total of "<< lowestRainfall <<" inches." << endl;
		cout << "The month with the highest rainfall is " << highestMonth << ", with a total of "<< highestRainfall <<" inches." << endl;

		//Reset the variables for re-run
		avgRainfall = 0;
		totalRainfall = 0;
		lowestRainfall = 0;
		highestRainfall = 0;
		highestMonth;
		lowestMonth;

		cout << "\nWould you like to run the program again? (Y/N) ";
		cin >> repeat;
		cout << endl;
	}while(repeat == 'y' || repeat == 'Y');

	return 0;
}
change line 47-48 to:
1
2
3
4
highestRainfall = monthlyTotals[0];
highestMonth = monthNames[0];
lowestRainfall = monthlyTotals[0];
lowestMonth = monthNames[0];

Moreover, following loop can be started from 1, not from 0;
Lines 76-77 are meaningless.
That worked out great, and I actually see where that would be a problem. Thank you!
Topic archived. No new replies allowed.