toal months off by one

This is going to sound like an incredibly stupid question but for some reason my months are incorrect. If I enter 1 year my months are off by 1 but when I enter 2 or more it is off by much more. All other calculations work fine.
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
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Declare variables
	int years, months, totalMonths = 0;
	double rainFall, totalRainFall = 0, averageRainFall;

	//Get input on years
	cout << "How many years do you want to calculcate rainfall for? ";
	cin >> years;

	//Verify years is not less than 1
	while (years < 1)
	{
		cout << "\nThat is an invalid number of years\n";
		cout << "Enter a years greater than zero: ";
		cin >> years;
	}

	cout << endl;

	//For statement for years
	for (int count = 1; count <= years; count++)
	{
		//For statement for months
		for (months = 1; months <= 12; months++)
		{
			//Get input on rainfall
			cout << "How many inches of rain fell in month " << months << "? ";
			cin >> rainFall;

			//Valiation rainfall is not less than zero
			while (rainFall < 0)
			{
				cout << "\nThat is an invalid inches of rainfall\n";
				cout << "Enter rainfall amount of at least zero: ";
				cin >> rainFall;
			}
			totalRainFall += rainFall;
			totalMonths += months;
		}
		
		cout << endl;
	}
	
	//Calculate average rainfall
	averageRainFall = totalRainFall / totalMonths;

	//show the results
	cout << "The total amount of months is " << totalMonths;
	cout << fixed << showpoint << setprecision(2);
	cout << "\nThe total amount of rainfall is " << totalRainFall << " inches.";
	cout << "\nThe average rainfall per month is " << averageRainFall << endl;

	return 0;
}
totalMonths += months;

Think about what this is actually doing. I don't think it's quite what you meant.
Last edited on
Im an idiot I figures out what I did wrong

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

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Declare variables
	int years, months, totalMonths = 0;
	double rainFall, totalRainFall = 0, averageRainFall;

	//Get input on years
	cout << "How many years do you want to calculcate rainfall for? ";
	cin >> years;

	//Verify years is not less than 1
	while (years < 1)
	{
		cout << "\nThat is an invalid number of years\n";
		cout << "Enter a years greater than zero: ";
		cin >> years;
	}

	cout << endl;

	//For statement for years
	for (int count = 1; count <= years; count++)
	{
		//For statement for months
		for (months = 1; months <= 12; months++)
		{
			//Get input on rainfall
			cout << "How many inches of rain fell in month " << months << "? ";
			cin >> rainFall;

			//Valiation rainfall is not less than zero
			while (rainFall < 0)
			{
				cout << "\nThat is an invalid inches of rainfall\n";
				cout << "Enter rainfall amount of at least zero: ";
				cin >> rainFall;
			}
			totalRainFall += rainFall;
			
		}
		totalMonths += 12;
		
		cout << endl;
	}
	
	//Calculate average rainfall
	averageRainFall = totalRainFall / totalMonths;

	//show the results
	cout << "The total amount of months is " << totalMonths;
	cout << fixed << showpoint << setprecision(2);
	cout << "\nThe total amount of rainfall is " << totalRainFall << " inches.";
	cout << "\nThe average rainfall per month is " << averageRainFall << endl;

	return 0;
The simplest thing to do would probably be to do totalMonths = years * 12;, but what you did works just as well.
Topic archived. No new replies allowed.