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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <iomanip>
usingnamespace 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;