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.
#include "stdafx.h"
#include <iostream>
usingnamespace 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.
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