Help me with this program

This is what I have to do:

Write and test a program that collects the total monthly rainfall for a one year period (12 months). The program should calculate and display the total rainfall for the year, the average monthly rainfall and the months with the highest and lowest rainfall. It should then ask if there is data for another year to be entered and repeat the process for the next year.

The program should loop and ask the user for each month's total rainfall.
Use a vector of doubles to hold the rainfall for a year.
Use a vector of strings to hold the names of the months.
Input Validation:

Do not accept negative numbers for monthly rainfall.

so far I've written most of the code but now I'm stuck.

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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

//Constant declarations
const int NUM_MONTHS = 12;
bool userWantsToContinue();

int main()
{
	vector<string> months(NUM_MONTHS);
	vector<double> rainfall(NUM_MONTHS);
	
	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10] = "November";
	months[11] = "December";

	//char (to be used) 

	do
	{

	for (int i=0; i < NUM_MONTHS; i++)
	{
	
	}
	double totalRain = 0.0;
	double avgRain = 0.0;
	int smallestRain = 0;
	int highestRain = 0;

	for (int currentMonth=0; currentMonth < NUM_MONTHS; currentMonth++)
	{
		totalRain = totalRain + rainfall[currentMonth];

		if (rainfall[currentMonth] < rainfall[smallestRain])
		{
			smallestRain = currentMonth;
		} else if (rainfall[currentMonth] > rainfall[highestRain])
		{
			highestRain = currentMonth;
		}
	}

	avgRain = totalRain / NUM_MONTHS;

	cout << endl;
	cout << "Total Rain: \t" << totalRain << endl;
	cout << "Average Rain: \t" << avgRain << endl;
	cout << "Month with least rain: " << months[smallestRain] << endl;
	cout << "Least monthly rain: " << months[highestRain] << endl;
	}
	
	while(userWantsToContinue);
	{
	
}
Last edited on
Seems to be the only thing you're missing is actually getting data from the user.
1
2
3
4
for (int i=0; i < NUM_MONTHS; i++) //your line 34
{
	cin >> rainfall[i];	
}


Now how do you think you would go about making sure it's not negative?
I made the rogram a bit more user friendly, hope you enjoy!

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

int main()
{
///Declarations
const int NUM_MONTHS = 12;
char Reset;
	vector<string> months(NUM_MONTHS);
	vector<double> rainfall(NUM_MONTHS);

	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10]= "November";
	months[11]= "December";
	///char (to be used)
	do{
    ///More Declarations
double totalRain = 0.0,avgRain = 0.0;
int smallestRain = 0,highestRain = 0,c=0;

	for (int i=0; i < NUM_MONTHS; i++){
    cout<<"Enter The amount of rainfall for "<<months[c]<<".\n> ";
	cin >> rainfall[i];
	c++;}
	for (int currentMonth=0; currentMonth < NUM_MONTHS; currentMonth++){
		totalRain = totalRain + rainfall[currentMonth];

		if (rainfall[currentMonth] < rainfall[smallestRain])
		{smallestRain = currentMonth;}else if (rainfall[currentMonth] > rainfall[highestRain]){highestRain = currentMonth;}}
	avgRain = totalRain / NUM_MONTHS;
	cout << endl<<endl;
	cout << "Total Rain: \t" << totalRain << endl;
	cout << "Average Rain: \t" << avgRain << endl;
	cout << "Month with least rain: " << months[smallestRain] << endl;
	cout << "Least monthly rain: " << months[highestRain] << endl;
	cout<<"Continue? Y/N\n> ";
	cin>>Reset;
	if(Reset=='N'||Reset=='n'){return 0;}
	}while(Reset!='N'||Reset!='n');
	return 0;}
Last edited on
Thank you so much guys! I really appreciate it!
Topic archived. No new replies allowed.