for loop not working correctly

This is a for loop (with a couple others nested inside) I'm using to output some averages for the following input file.
But for some reason the main loop doesn't function correctly. It will output correctly but on the last time through it keeps outputting the last averages what seems like an infinite number of times. Is there a flaw in my code that I'm missing? I've checked it multiple times and I can't see what's wrong with it. If any more info is needed I will be glad to provide it... thanks for the help.
# Stus		# Progs		# Tests
   10		    3		    3
  ID 		P1	P2	P3	T1	T2	T3
904084117	100	100	100	100	78	86
904254870	95	100	100	0	84	87
904278054	82	95	82	100	56	61
905299003	95	100	90	100	57	78
903191606	100	95	100	90	50	35
906354053	100	95	90	100	87	42
906445013	90	95	100	90	78	67
906729792	97	95	100	100	60	73
900299833	100	100	100	0	90	67
905158302	85	90	91	100	57	49


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	for (numLoops = 0; numLoops < students; numLoops++) {
		progtotal = 0;
		testtotal = 0;
		In >> studentid;
		Out << studentid << "     ";

		for (numLoops = 0; numLoops < programs; numLoops++) {
			In >> progscore;
			progtotal = progtotal + progscore;
			progavg = progtotal / programs;
		}

		Out << progavg << "         ";

		for (numLoops = 0; numLoops < tests; numLoops++) {
			In >> testscore;
			testtotal = testtotal + testscore;
			testavg = testtotal / tests;
		}

		Out << testavg << endl;
	}
closed account (Lv0f92yv)
It looks like (without compiling and testing) that every time that first for loop is being done, numLoops will always start at 'tests' and increment by 1, because each time through the loop, the last thing that happens is numLoops is equal to tests:

Last nested loop:

1
2
3
4
5
for (numLoops = 0; numLoops < tests; numLoops++) {
			In >> testscore;
			testtotal = testtotal + testscore;
			testavg = testtotal / tests;
		}


Now, numLoops == tests; and will be incremented by 1 by:

1
2
3
for (numLoops = 0; numLoops < students; numLoops++) {
		progtotal = 0;
...



In any event, try using different variables for iterating - if for no other reason than better style and easier debugging.
Last edited on
Gotcha... thanks for the reply. I'll give it a try a little later and post something back.
closed account (Lv0f92yv)
Good luck!
Thank you it did work. I should probably post this next question on another topic but I feel like it's a relatively simple one so I'll ask it right here. Now I need to close and reopen my input file but I can't seem to do that. I invoked In.clear() and In.close() and tried reopening it but it still doesn't work... thanks for the help.
What is the code you have for closing then opening the file?
After the loop I simply just do:
1
2
3
In.close()
In.clear()
In.open("filename")

This has worked for me in the past I don't know why it isn't now.
Is 'filename' a variable you assigned earlier?
No, filename is the name of the input file I'm using which in this case happens to be scores.dat. So it would be:
In.open("scores.dat");
Possibly it could be the In.clear(); you have.

Cause I quickly keyed a .open > .close > .open on a simple sample prog and it worked fine.
I'll try it out... if it doesn't work I'll figure something out, it's not too big of a deal... thanks.
Topic archived. No new replies allowed.