What's wrong with my code?

First semester of programming. I am stuck on an assignment. Please ignore the averages and stuff at the very bottom, will look at that later. What I am worried about are my loops.
Whenever I debug and put in year as "1", everything is fine. If I enter anything higher than that, for example: "2" for number of years, it goes to 24, which is what I want, but then it starts at 1 again and goes another 24.
Enter 3 and it goes to 36, good, and then it starts at 1 and goes another 36. and so on...
I've been at this for a while. Thanks.

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

  #include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	//Variables
	int numYears,
		totalMonths = 0;
	double inches,
		avgRain,
		totalRain = 0.0; // Accumulator

	totalMonths = numYears * 12;

	cout << "This program will calculate the average rainfall over a period of years."; //Prompts user for number of years.
	cout << "Please enter the number of years: ";
	cin >> numYears;

	while (numYears <= 0) //Prevents user from setting years equal to or less than 0.
	{
		cout << "Number of years is invalid, please re-enter a valid number of years: ";
		cin >> numYears;
	}

	for (int years = 1; years < numYears; years++)
	{

		for (int months = 1; months <= 12; months++) //iterate for the months in a year.
		{
			cout << "Please enter the amount of rainfall for month " << months << " in inches: ";
			cin >> inches;
			totalRain = totalRain + inches;
			

			while (inches < 0) //Prevents user from entering a negative value for inches.
			{
				double inches;
				cout << "Please enter a valid amount of rainfall for month " << months << "in inches: ";
				cin >> inches;
			}
		}
	}

	avgRain = totalRain / totalMonths; //Calculates the average amount of rainfall during that period.

	cout << "Number of months during period: " << totalMonths << endl;
	cout << "Total amount of rainfall: " << totalRain << endl;
	cout << "Average rainfall for period: " << avgRain << endl;


	return 0;
}

Could you explain in more detail what the assigment is?
Also there are small things to be improved.
totalRain = totalRain + inches;
can be rewritten with
totalRain+=inches;
Will edit the post to see if i can find the solution.
Last edited on
Nevermind, found your mistake.
1
2
3
4
5
6
7
8
9
10
11
	int numYears,
		totalMonths = 0;
	double inches,
		avgRain,
		totalRain = 0.0; // Accumulator

	totalMonths = numYears * 12;

	cout << "This program will calculate the average rainfall over a period of years."; //Prompts user for number of years.
	cout << "Please enter the number of years: ";
	cin >> numYears;


Hope this helps you!
Just to prove i am right. After cin>>numYears. cout the totalMonths
Explanation:
totalMonths is being multiplied by numYears*12 BEFORE we know how much is numYears.
here would be the correct code.
1
2
3
4
5
6
7
8
9
10
	int numYears,
		totalMonths = 0;
	double inches,
		avgRain,
		totalRain = 0.0; // Accumulator

	cout << "This program will calculate the average rainfall over a period of years."; //Prompts user for number of years.
	cout << "Please enter the number of years: ";
	cin >> numYears; //we get the value of numyears then we multiply by 12 to get total months
totalMonths = numYears * 12;//Moved this from up there 

Also just noticed: the first for loop needs to go until years<=numYears
Last edited on
EDIT
Sorry, never mind.
I did what you suggested then, I copied my code into a new window and it worked. Thank you!!!
Last edited on
Registered users can post here. Sign in or register to post.