so i think we might have nested loops in my upcoming final.

so im practicing some challanges.

heres a program sample

For how many years have you collected rainfall data? 2
Enter the rainfall (in inches) for month 1 of year 1: 4
Enter the rainfall (in inches) for month 2 of year 1: 5.2
Enter the rainfall (in inches) for month 3 of year 1: 4.7
Enter the rainfall (in inches) for month 4 of year 1: 3.6
Enter the rainfall (in inches) for month 5 of year 1: 2.3
Enter the rainfall (in inches) for month 6 of year 1: .6
Enter the rainfall (in inches) for month 7 of year 1: .1
Enter the rainfall (in inches) for month 8 of year 1: .3
Enter the rainfall (in inches) for month 9 of year 1: 2.4
Enter the rainfall (in inches) for month 10 of year 1: 3.7
Enter the rainfall (in inches) for month 11 of year 1: 5.2
Enter the rainfall (in inches) for month 12 of year 1: 7.1
Enter the rainfall (in inches) for month 1 of year 2: 5.2
Enter the rainfall (in inches) for month 2 of year 2: 6.5
Enter the rainfall (in inches) for month 3 of year 2: 4.4
Enter the rainfall (in inches) for month 4 of year 2: 6.8
Enter the rainfall (in inches) for month 5 of year 2: 5.1
Enter the rainfall (in inches) for month 6 of year 2: 2.1
Enter the rainfall (in inches) for month 7 of year 2: 0
Enter the rainfall (in inches) for month 8 of year 2: 0
Enter the rainfall (in inches) for month 9 of year 2: .9
Enter the rainfall (in inches) for month 10 of year 2: 2.6
Enter the rainfall (in inches) for month 11 of year 2: 5.1
Enter the rainfall (in inches) for month 12 of year 2
: 4.2

The total number of months of rainfall data: 24
The total inches of rainfall for that period: 82.1
The average rainfall per month for that period: 3.42083

i just need to know how to do that blod bold part. I could just do it by rainfall for month 1, 2, 3, 4, 18, 20, 21, 22, 23, 24 (2 years = 24 months) but i want it if i put 2 years to show 12 months then the other 12 months for the next year.

heres my code so far

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
		
		double total = 0.0;// total rainfall inches
		int numData;//number of years collected rainfall data
		

		cout <<"For how many years have you collected rainfall data? ";
		cin >> numData;

	

		for (int month = 1; month <= numData; month++)
		{	float inches;

		cout <<"Enter the rainfall (in inches) for month" << month << ": ";
		cin >> inches;

		}

	
		system("pause");
		return 0;

Recommended Answers

All 17 Replies

use nested loops

for(int year = 1; year < 2; year++)
{
    for(int month = 1; month <= 12; month++)
    {
        // blabla
    }
}
commented: thanks +1

thanks dude that helped a lot but idk it didnt want to cout the months for the 2nd year. i put 2 years and it only gaved me the first 12 months of year 1.

heres my updated code

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
		
		double total = 0.0;// total rainfall inches
		int numData;
			
		cout <<"For how many years have you collected rainfall data? ";
		cin >> numData;

	for(int year = 1; year < 2; year++)
{
    for(int month = 1; month <= 12; month++)
		{
		float inches;

        cout <<"Enter the rainfall (in inches) for month" << month << ": ";
		cin >> inches;

		}
}

Either that or

for(int month = 0; month < numYears*12; month++) {
     std::cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << std::endl;
     // do what you want
}

This assuming I understood your question.

Please read here why such thing as asap is to be avoided.

ok thanks for the help seems like im getting it. sorry for putting "asap" :D

one question to mr boolf when i put my input when it says "of year1" i cant seem to give it a space. like i out "of year 13.4 i want it to look of year 1: 3.4 seems easy to do but im having trouble

I don't understand what do you mean - I put an endline before cin.

Post your code and explain where do you want to put the space (i.e. how is the output and how you'd like it to be)

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
		
		double total = 0.0;// total rainfall inches
		int numData;
			
		cout <<"For how many years have you collected rainfall data? ";
		cin >> numData;

for(int month = 0; month < numData*12; month++) 

{     
		float inches;
		cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
		 << (month/12) + 1;     
		cin >> inches;
}


			

		
		

	
		system("pause");
		return 0;
}

alright lets say for the first input you type 3.2 it looks like 13.2 because the "1" of year isnt spaced

Well you cut off the endline - put it back or simply insert a space

cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year " << (month/12) + 1 << ": ";

Was it hard?

commented: thanks +1

LOL dumb me
thanks a lot.

hmm i seem to be stuck (no surprise) lol

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
		
		double totalRain = 0.0;// total rainfall inches
		double totalMonths = 0.0;
		int numData;
			
		cout <<"For how many years have you collected rainfall data? ";
		cin >> numData;

for(int month = 0; month < numData*12; month++) 

{     
		float inches;
		cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
		 << (month/12) + 1  << ": ";   
		cin >> inches;

		totalRain += inches;
		totalMonths = 
}
cout <<"The total number of months of rainfall data:" << totalMonths << endl;
cout <<"The total inches of rainfall for that period:" << totalRain << endl;
			

system("pause");
return 0;
}

i was able to get the total inches of rain but idk how to do this part

The total number of months of rainfall data: 24 (saying i put 2 years)
The total inches of rainfall for that period: 82.1
The average rainfall per month for that period: 3.42083

You want totaMonths to be the months that you entered data for? You don't need the variable, either use numYears*12 or declare month outside the loop and then use its last value.

ohh ok yeah numYears * 12 works perfect.
i was also able to calcluate the avg damn that was a bit hard. once again thanks for the help. :D

anyone know why this isnt working?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
		
		double totalRain = 0.0;// total rainfall inches
		double count = 0.0;
		int numData;
		char answer;
			
		cout <<"For how many years have you collected rainfall data? ";
		cin >> numData;
			if (numData < 1)
			{	
				cout <<"Invalid:  number of years must be at least one." << endl;
				cout <<"Do you want to try again (y/n)?";
				cin >> answer;
					{
						if (answer == 'Y' || answer == 'y')
						cout <<"For how many years have you collected rainfall data? ";
						cin >> numData;
						if (answer != 'Y' || answer != 'y')
						 cout <<"Goodbye";
					}
			}

for(int month = 0; month < numData*12; month++) 

{     
		float inches;
		cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
		 << (month/12) + 1  << ": ";   
		cin >> inches;

		count++;
		totalRain += inches;
		
		
}
cout <<"The total number of months of rainfall data:" << numData * 12 << endl;
cout <<"The total inches of rainfall for that period:" << totalRain << endl;
cout <<"The average rainfall per month for that period:" << totalRain / (float)count << endl;
			

system("pause");
return 0;
}

the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.

ex

For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1

You didn't tell it to do so, that's why it would not terminate the program.

if(choice=='Y') {
     // do something
}
else {
     return EXIT_FAILURE;
}

You should put your input validation in a loop though - what if the user enters two times consecutively a number of years <= 0 ?

Also you don't need count variable: you already have totalMonths (i.e. numData * 12).

cool thanks!

and actually im using count variable for the avg and it works fine.

Yes but count == totalMonths :P

(sorry missed second page of comments)

well hope this helps...i ddnt check the other guys...just wanted 2 do smting cuz i was bored...if u find something wrong tell me...thnx... xD

#include <iostream.h>
int nb_of_year;
int i,j,n;
double r_inches;//rainfall inches
int totalm=0;//total months
double total_i;//total inches
double averagei;


int main()
{
cout << "enter the number of years u wanna do the calculations for :\n";
cin >> n;

	for (i=1;i<=n;i++)
	{
		for (j=1;j<=12;j++)
		{
			cout << "Enter the rainfall (in inches) for month "<< j << " of year ";
			cout << i << ":\n";
			cin >> r_inches;

				total_i+=r_inches;
				
				totalm+=1;

		}

	}

	
	cout << "The total number of months of rainfall data :" << totalm << endl;
	cout << "The total inches of rainfall for that period:" << total_i << endl;
	averagei=total_i/totalm;

	cout << "The average rainfall per month for that period is:" << averagei <<endl;



return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.