nested for loops and reading from input file

I have an assignment and i'm so close to throwing my computer against the wall because I can't freaking seem to get it.

the assignment is:

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of
years. The program should first input the number of years. The outer for-loop will iterate once for
each year. The inner for-loop will iterate twelve times, once for each month. Each iteration of the
inner loop will input the inches of rainfall for that month.

so far I have this:
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>   // input output
#include <iomanip>    // manipulator
#include <cmath>      // math
#include <string>     // string
#include <fstream>    // file stream


using namespace std;


int main()

{
 
 // declare variables
 int outerLoop;
 int innerLoop;
 int numberOfYears;
 int rainAmount;
 ifstream inrain;
 ofstream outrain; 
 
 
 inrain.open("inrain.txt");    // open input text
 outrain.open("outrain.txt");  // open output text

 inrain >> numberOfYears;

 for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
 { 
   outrain << fixed << endl;
   outrain << "Year " << outerLoop << endl;
  
  
  for(innerLoop = 1; innerLoop <= 12; innerLoop++)
  {
   inrain >> rainAmount;
   
 
   outrain << showpoint << setprecision(2) << endl;
   outrain << "Inches of rain for month " << innerLoop << " =" << rainAmount << endl;

  }
  
outrain << "The total amount of rainfall in this year was " << rainAmount << endl;

 }

 
 cout << "To view results, open the outrain.txt file in the same directory" << endl;
 cout << "as the source code for this program." << endl;
    
    
inrain.close();  // close input text
outrain.close(); // close output text
    
system("PAUSE");
return 0;    
}


My main problem is once in the loop continuing to read the remaining integers from the input file as to match them with the month. I'm at a stand still, and fustrated and obviously have no idea what i'm missing.
Can someone please point me in the right direction.

I don't see any calculations in your program. After you get to month 12 in the inner loop, you're just going to re-write the latest rainAmount you read in from inrain.
I didn't think I needed any calculations.
Basically the input file contains :
3
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
0.0
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
12.0
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.11
2.12

with "3" being the number of years which is emphasized in the first for loop.
the information after the number 3 is what needs to be paired up with the 12 months per 3 years. so total there's 36 pieces of data. I don't know if I need another for loop or how to write it.

It didn't seem like i needed calculations I thought I just needed to somehow get back to the input file to get that data.
How did you expect to get the average without calculations? All the input file is giving you is raw data.

For each year, you need to:

clear an accumulator variable
read in all the months for that year, and add them to the accumulator
divide the accumulator by 12 to get the average.

I don't think you need to write to the output file each monthly datum, either...just the average you derived.

Also, your input is floating-point, so storing in integers is going to cause some problems.
okay if I'm hearing you correctly my code should look something like this:

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
62
63
64
65
66
67
#include <iostream>   // input output
#include <iomanip>    // manipulator
#include <cmath>      // math
#include <string>     // string
#include <fstream>    // file stream


using namespace std;


int main()

{
 
 // declare variables
 int outerLoop;
 int innerLoop;
 int numberOfYears;
 double average;
 double rainAmount;
 ifstream inrain;
 ofstream outrain; 
 
 
 inrain.open("inrain.txt");    // open input text
 outrain.open("outrain.txt");  // open output text

 inrain >> numberOfYears;
 
 for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
 { 
   numberOfYears = 3;
   innerLoop = 12;
   rainAmount = numberOfYears + innerLoop;
   outrain << fixed << endl;
   outrain << "Year " << outerLoop << endl;
  
  inrain >> rainAmount;
  for(innerLoop = 1; innerLoop <= 12; innerLoop++)
  {
   inrain >> rainAmount;
   
   outrain << showpoint << setprecision(2) << endl;
   outrain << "Inches of rain for month " << innerLoop << " =" << rainAmount << endl;

  }
  inrain >> rainAmount;
  average = rainAmount / 12;
  outrain << "The total amount of rainfall in this year was " << average << endl;


 }

 
 cout << "To view results, open the outrain.txt file in the same directory" << endl;
 cout << "as the source code for this program." << endl;
    
    
inrain.close();  // close input text
outrain.close(); // close output text
    
system("PAUSE");
return 0;    
}


    


my output looks like:


Year 1

Inches of rain for month 1 =0.20

Inches of rain for month 2 =0.30

Inches of rain for month 3 =0.40

Inches of rain for month 4 =0.50

Inches of rain for month 5 =0.60

Inches of rain for month 6 =0.70

Inches of rain for month 7 =0.80

Inches of rain for month 8 =0.90

Inches of rain for month 9 =1.00

Inches of rain for month 10 =1.10

Inches of rain for month 11 =1.20

Inches of rain for month 12 =0.00
The total amount of rainfall in this year was 0.08

Year 2

Inches of rain for month 1 =1.20

Inches of rain for month 2 =1.30

Inches of rain for month 3 =1.40

Inches of rain for month 4 =1.50

Inches of rain for month 5 =1.60

Inches of rain for month 6 =1.70

Inches of rain for month 7 =1.80

Inches of rain for month 8 =1.90

Inches of rain for month 9 =12.00

Inches of rain for month 10 =2.00

Inches of rain for month 11 =2.10

Inches of rain for month 12 =2.20
The total amount of rainfall in this year was 0.19

Year 3

Inches of rain for month 1 =2.50

Inches of rain for month 2 =2.60

Inches of rain for month 3 =2.70

Inches of rain for month 4 =2.80

Inches of rain for month 5 =2.90

Inches of rain for month 6 =2.11

Inches of rain for month 7 =2.12

Inches of rain for month 8 =2.12

Inches of rain for month 9 =2.12

Inches of rain for month 10 =2.12

Inches of rain for month 11 =2.12

Inches of rain for month 12 =2.12
The total amount of rainfall in this year was 0.18

but it should look like: (ignoring the alignment).
Data for year 1
Number of inches for month 1: 0.10
Number of inches for month 2: 0.20
Number of inches for month 3: 0.30
Number of inches for month 4: 0.40
Number of inches for month 5: 0.50
Number of inches for month 6: 0.60
Number of inches for month 7: 0.70
Number of inches for month 8: 0.80
Number of inches for month 9: 0.90
Number of inches for month 10: 1.00
Number of inches for month 11: 1.10
Number of inches for month 12: 1.20


Data for year 2
Number of inches for month 1: 0.00
Number of inches for month 2: 1.00
Number of inches for month 3: 1.10
Number of inches for month 4: 1.20
Number of inches for month 5: 1.30
Number of inches for month 6: 1.40
Number of inches for month 7: 1.50
Number of inches for month 8: 1.60
Number of inches for month 9: 1.70
Number of inches for month 10: 1.80
Number of inches for month 11: 1.90
Number of inches for month 12: 12.00


Data for year 3
Number of inches for month 1: 2.00
Number of inches for month 2: 2.10
Number of inches for month 3: 2.20
Number of inches for month 4: 2.30
Number of inches for month 5: 2.40
Number of inches for month 6: 2.50
Number of inches for month 7: 2.60
Number of inches for month 8: 2.70
Number of inches for month 9: 2.80
Number of inches for month 10: 2.90
Number of inches for month 11: 2.11
Number of inches for month 12: 2.12


Over a period of 36 months, 63.03 inches of rain fell.
Average monthly rainfall for the period is 1.75 inches.

if i read in all the months for that year, do i have to give number of months a variable of its own?
OKAY WAIT! i think i got it.
I changed my code to this:

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
62
63
64
65
66
67
68
#include <iostream>   // input output
#include <iomanip>    // manipulator
#include <cmath>      // math
#include <string>     // string
#include <fstream>    // file stream


using namespace std;


int main()

{
 
 // declare variables
 int outerLoop;
 int innerLoop;
 int numberOfYears;
 double average;
 double rainAmount;
 ifstream inrain;
 ofstream outrain; 
 
 
 inrain.open("inrain.txt");    // open input text
 outrain.open("outrain.txt");  // open output text

 inrain >> numberOfYears;
 
 for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
 { 
   numberOfYears = 3;
   innerLoop = 12;
   
   outrain << fixed << endl;
   outrain << "Year " << outerLoop << endl;
  
  
  for(innerLoop = 1; innerLoop <= 12; innerLoop++)
  {
   inrain >> rainAmount;
  
   
   outrain << showpoint << setprecision(2) << endl;
   outrain << "Inches of rain for month " << innerLoop << " =" << rainAmount << endl;

  }
  inrain >> rainAmount;
  average = (numberOfYears + innerLoop ) / 12;
  outrain << "The total amount of rainfall in this year was " << average << endl;


 }

 
 cout << "To view results, open the outrain.txt file in the same directory" << endl;
 cout << "as the source code for this program." << endl;
    
    
inrain.close();  // close input text
outrain.close(); // close output text
    
system("PAUSE");
return 0;    
}


    


and my results were this:


Year 1

Inches of rain for month 1 =0.10

Inches of rain for month 2 =0.20

Inches of rain for month 3 =0.30

Inches of rain for month 4 =0.40

Inches of rain for month 5 =0.50

Inches of rain for month 6 =0.60

Inches of rain for month 7 =0.70

Inches of rain for month 8 =0.80

Inches of rain for month 9 =0.90

Inches of rain for month 10 =1.00

Inches of rain for month 11 =1.10

Inches of rain for month 12 =1.20
The total amount of rainfall in this year was 0.00

Year 2

Inches of rain for month 1 =1.00

Inches of rain for month 2 =1.10

Inches of rain for month 3 =1.20

Inches of rain for month 4 =1.30

Inches of rain for month 5 =1.40

Inches of rain for month 6 =1.50

Inches of rain for month 7 =1.60

Inches of rain for month 8 =1.70

Inches of rain for month 9 =1.80

Inches of rain for month 10 =1.90

Inches of rain for month 11 =12.00

Inches of rain for month 12 =2.00
The total amount of rainfall in this year was 0.18

Year 3

Inches of rain for month 1 =2.20

Inches of rain for month 2 =2.30

Inches of rain for month 3 =2.40

Inches of rain for month 4 =2.50

Inches of rain for month 5 =2.60

Inches of rain for month 6 =2.70

Inches of rain for month 7 =2.80

Inches of rain for month 8 =2.90

Inches of rain for month 9 =2.11

Inches of rain for month 10 =2.12

Inches of rain for month 11 =2.12

Inches of rain for month 12 =2.12
The total amount of rainfall in this year was 0.18



which means Im probably off with the arithmatic at some point. what do you think?
You're getting closer. But you still need an accumulator. In the outer loop, set it to 0.0. In the inner loop, add the monthly rainfall to it. And in the outer loop, after the inner loop, take the average by dividing the accumulated amount by 12.
okay my current code is:

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>   // input output
#include <iomanip>    // manipulator
#include <cmath>      // math
#include <string>     // string
#include <fstream>    // file stream


using namespace std;


int main()

{
 
 // declare variables
 int outerLoop;
 int innerLoop;
 int numberOfYears;
 double counter;
 double average;
 double rainAmount;
 ifstream inrain;
 ofstream outrain; 
 
 
 inrain.open("inrain.txt");    // open input text
 outrain.open("outrain.txt");  // open output text

 inrain >> numberOfYears;
 
 for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
 { 
   counter = 0.0;
   numberOfYears = 3;
   innerLoop = 12;
   
   outrain << fixed << endl;
   outrain << "Year " << outerLoop << endl;
   
  for(innerLoop = 1; innerLoop <= 12; innerLoop++)
  {
   inrain >> rainAmount;
   
   outrain << showpoint << setprecision(2) << endl;
   outrain << "Inches of rain for month " << innerLoop << ": " << rainAmount << endl;
  }
  
 }
 average = (innerLoop * rainAmount) / 12;
outrain << "The total amount of rainfall for 36 months is: " << average << endl;
 
 cout << "To view results, open the outrain.txt file in the same directory" << endl;
 cout << "as the source code for this program." << endl;
    
    
inrain.close();  // close input text
outrain.close(); // close output text
    
system("PAUSE");
return 0;    
}


and my results are:

Year 1

Inches of rain for month 1: 0.10

Inches of rain for month 2: 0.20

Inches of rain for month 3: 0.30

Inches of rain for month 4: 0.40

Inches of rain for month 5: 0.50

Inches of rain for month 6: 0.60

Inches of rain for month 7: 0.70

Inches of rain for month 8: 0.80

Inches of rain for month 9: 0.90

Inches of rain for month 10: 1.00

Inches of rain for month 11: 1.10

Inches of rain for month 12: 1.20

Year 2

Inches of rain for month 1: 0.00

Inches of rain for month 2: 1.00

Inches of rain for month 3: 1.10

Inches of rain for month 4: 1.20

Inches of rain for month 5: 1.30

Inches of rain for month 6: 1.40

Inches of rain for month 7: 1.50

Inches of rain for month 8: 1.60

Inches of rain for month 9: 1.70

Inches of rain for month 10: 1.80

Inches of rain for month 11: 1.90

Inches of rain for month 12: 12.00

Year 3

Inches of rain for month 1: 2.00

Inches of rain for month 2: 2.10

Inches of rain for month 3: 2.20

Inches of rain for month 4: 2.30

Inches of rain for month 5: 2.40

Inches of rain for month 6: 2.50

Inches of rain for month 7: 2.60

Inches of rain for month 8: 2.70

Inches of rain for month 9: 2.80

Inches of rain for month 10: 2.90

Inches of rain for month 11: 2.11

Inches of rain for month 12: 2.12
The total amount of rainfall for 36 months is: 2.30

Im in route to completing it, I know I need to go back and look at my accumulator, my focus is just off with looking at this. Thank you SOOOOO SOOO SOO much for helping me thus far though, I seriously don't think I would have gotten this far without your assistance. I'll look at this again tomorrow.
Last edited on
I think you're getting the hang of it. I may have misunderstood your assignment when I first read it. I thought you were supposed to output an average on a year-by-year basis.

If this is true, you need to move your averaging code inside your outer loop.

If this is NOT true, you need to accumulate the total for all years, not just the last year, and take your average accordingly. (You still have some work to do with your accumulator logic.) I'm also not sure that you need two loops for this assignment if this is the case. You can just read in all the rainfall, keep adding it to the accumulator, and take the average at the end. No need to do anything year by year that I can see.
Yeah he wants nested loops so for each year 12 months is equated with it and so the data on the input file (inches of rainfall) can be matched with each month.

And yes I only need the average for the 3 years total, so the average of the 36 pieces of info on the input file.

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
inrain.open("inrain.txt");    // open input text
 outrain.open("outrain.txt");  // open output text

 inrain >> numberOfYears;
 
 for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
 { 
   counter = 0.0;
   numberOfYears = 3;
   innerLoop = 12;
   
   outrain << fixed << endl;
   outrain << "Year " << outerLoop << endl;
   
  for(innerLoop = 1; innerLoop <= 12; innerLoop++)
  {
   inrain >> counter;
   
   outrain << showpoint << setprecision(2) << endl;
   outrain << "Inches of rain for month " << innerLoop << ": " << counter << endl;
  }
  
 }
 average = (counter * numberOfYears) / 12;
outrain << "The total amount of rainfall for 36 months is: " << average << endl;
 
 cout << "To view results, open the outrain.txt file in the same directory" << endl;
 cout << "as the source code for this program." << endl;
    
    
inrain.close();  // close input text
outrain.close(); // close output text 


right now it looks like this. Im not exactly sure on how else I should be gathering the information. If it's already collecting the data in the outer loop per year, per month rotation with the current counter I thought I should just be able to place the "counter" variable in the string for the month for loop to get that info.
Your counter needs to be initialized outside the loop, otherwise you're just throwing away the data from all but the final year. (My fault; I thought you were doing a year-by-year average.)

And you're not adding the input to the counter anywhere. You're reading in the rain data, but not doing anything with it. Your "inrain >> counter" doesn't ADD to the counter; it just gives it the most recent value.

Finally, your average calculation is off. When you're done, counter should contain the sum of the three years' rain. So, you want to divide it by the number of samples to derive an average.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	inrain >> numberOfYears;
	counter = 0.0;

	for(outerLoop = 1; outerLoop <= numberOfYears; outerLoop++)
	{
		numberOfYears = 3;
		innerLoop = 12;

		outrain << fixed << endl;
		outrain << "Year " << outerLoop << endl;

		for(innerLoop = 1; innerLoop <= 12; innerLoop++)
		{
			inrain >> rainAmount;
			outrain << showpoint << setprecision(2) << endl;
			outrain << "Inches of rain for month " << innerLoop << ": " << rainAmount << endl;
			counter += rainAmount;
		}

	}
	average = counter / (12 * numberOfYears);

	outrain << "The total amount of rainfall for 36 months is: " << average << endl;
IT WORKS IT WORKS!!! You have just been so amazing and patient with me thank you so much. I'm actually going to go back and see if I can do it again and comprehend what I wasn't getting before, but thank you so very much.
Glad to help. Have fun...
Topic archived. No new replies allowed.