Review: Average Rainfall

Hello, I am having problems writing this program. I will show you..



/*Write a program that calculates the average rainfall for three months. The program
should ask the user to enter the name of each month, such as June or July, and the
amount of rainfall (in inches) that fell each month. The program should display a mes-
sage similar to the following:

The average rainfall for June, July, and August is 6.72 inches.*/
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char month1[8], month2[8], month3[8];
double rainfall, avg_Rainfall;

cout << "Enter three consecutive months: " << endl;
cin >> month1 >> month2 >> month3;

cout << "How much inches of rain fell in " << month1 << "?" << endl;
cin >> rainfall;

cout << "How much inches of rain fell in " << month2 << "?" << endl;
cin >> rainfall;

cout << "How much inches of rain fell in " << month3 << "?" << endl;
cin >> rainfall;

system("pause");
return 0;
}


I was able to complete most of it, but i was confused on how to take the average of the three rainfalls for each month.. Maybe i'm over thinking it, but I would like your opinion on where I went wrong. Thank you
avgRainfall = totalRainfall / numberOfMonths;

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
/*Write a program that calculates the average rainfall for three months. The program
should ask the user to enter the name of each month, such as June or July, and the
amount of rainfall (in inches) that fell each month. The program should display a mes-
sage similar to the following:

The average rainfall for June, July, and August is 6.72 inches.*/
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   char month1[8], month2[8], month3[8];
   double rainfall, avg_Rainfall;
   double totalRainfall = 0; //create a variable to hold the total rainfall

   cout << "Enter three consecutive months: " << endl;
   cin >> month1 >> month2 >> month3;

   cout << "How much inches of rain fell in " << month1 << "?" << endl;
   cin >> rainfall;
   totalRainfall += rainfall; //add this month's rainfall to the total rainfall, then store the result back into totalRainfall, ie:
   // totalRainfall = totalRainfall + rainfall;

   cout << "How much inches of rain fell in " << month2 << "?" << endl;
   cin >> rainfall;
   totalRainfall += rainfall; //same thing here

   cout << "How much inches of rain fell in " << month3 << "?" << endl;
   cin >> rainfall;
   totalRainfall += rainfall; //and here

   avg_Rainfall = totalRainfall / 3; //now calculate the average
   //...

   system("pause");
   return 0;
}
oh i see, thank you for your help
Topic archived. No new replies allowed.