Error, seeking help

So I'm trying to find the average on 3 months of rainfall. Whenever I run this code, it asks for the first month only.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	double month1, month2, month3;
	double average = month1 + month2 + month3 / 3;
	
	cout << "Enter the first month: " << endl;
	cin >> month1;
	cout << "Enter the second month: " << endl;
	cin >> month2;
	cout << "Enter the third month: " << endl;
	cin >> month3;

	cout << "The average rainfall is " << average << endl;
	return 0;
	
}
What are you entering for that first month?

Also wouldn't you get better results for the average if you did the calculation after you got the values from the user?

You need to do the average calculation after getting values for the months. Should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	double month1, month2, month3;
	double average;

	cout << "Enter the first month: " << endl;
	cin >> month1;
	cout << "Enter the second month: " << endl;
	cin >> month2;
	cout << "Enter the third month: " << endl;
	cin >> month3;

	average = month1 + month2 + month3 / 3;

	cout << "The average rainfall is " << average << endl;
	return 0;

}
Topic archived. No new replies allowed.