Least and Greatest for Array problem?

Hey guys, how can I find the lowest and highest number for the rainfall? I tried some things that lead to nowhere. Normally I can solve a problem like this because I don't have a limitation of the number input being 0 or more (such as this program). How can I make this work properly?

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
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	double total = 0;
	double Greatest = 0;
	string greatest;
	string least;
	double Least = 0;
	const int months = 12;
	double rainfall[months];
	string Months[months] = {"January", "February", "March", "April", "May", "June", "July"
		, "August", "September", "October", "November", "December"};

	cout << "In this program you will be entering data for each month (in inches)." << endl;
	cout << "Please enter the data..." << endl;
	
	for (int x = 0; x < months; x++)
	{
		cout << "Enter rainfall (inches) for " << Months[x] << ": ";
		cin >> rainfall[x];
		
		while (rainfall[x] < 0)
		{
			cout << "Invalid. Rainfall must be 0 inches or more." << endl;
			cin.clear();
			cin.ignore();
			cin >> rainfall[x];
		}

		if (rainfall[x] > 0)
		{
			Greatest = rainfall[x];
			greatest = Months[x];
		}
		else if (rainfall[x] < 0)
		{
			Least = rainfall[x];
			least = Months[x];
		}

		total += rainfall[x];
	}

	double average;
	average = total/months;

	cout << "\nThe total rainfall for the year was " << total << " inches." << endl;
	cout <<"The monthly average for the year was " << average << " inches." << endl;
	cout <<"\nThe month with the most rainfall was " << greatest << " with " << Greatest << " inches." << endl;
	cout <<"The month with the least rainfall was " << least << " with " << Least << " inches." << endl;

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.