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 << "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
/*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>
usingnamespace 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;
}