while loop to read to EOF

Apr 14, 2013 at 10:59pm
I am trying to read a file into an array[50] that has a students name followed by their grade. Example of the input file: Bob 99.5 Kim 87.3 Jack 94.8 Kyle 98.5 ect...
I do not know how many entries are in the file and I need to find the average grade there for I must count the entries as they are read in. so far my code looks like this:
1
2
3
4
5
6
7
8
9
10
	counter = 1;
	inFile >> studentName[0];
	   while (inFile)	
	      {
		inFile >> studentName[counter];
		inFile >> testScores[counter];
		counter++;	
	      }
	cout<<counter; //<-- tels me how many grades were read in.
	cout << endl;


the problem I am having is that cout << counter is returning 2 when there are 10 grades to be read.
Last edited on Apr 14, 2013 at 11:06pm
Apr 14, 2013 at 11:12pm
Line 2 is not needed.
counter should start from 0, not 1.

Because there are two consecutive reads of studentName[] the score (numeric value) gets stored in the name, then the program attempts to read a name (non-numeric) into the testScores[]. That will fail, and the loop terminates.
Apr 14, 2013 at 11:13pm
Last edited on Apr 14, 2013 at 11:14pm
Apr 14, 2013 at 11:35pm
If I remove line 2 and start the counter at 0 won't that make the while(infile) return false and skip the loop all together?
as for the two consecutive reads of studentName[] ... I feel silly. Sometimes the problem is plain as day and you still can't see it. I changed the code to read the score in after the name and bingo. Thank you for your help. I would have spent half of the day and still wouldn't have figured it out. You are a lifesaver
1
2
3
4
5
6
7
8
9
10
11
12
	counter = 0;
	infile >> studentName[0];
	infile >> testScores[0];
	  while (infile)	
		{
                        counter++;
			infile >> studentName[counter];
			infile >> testScores[counter];				
		}

	cout<<counter;
	cout << endl;
Last edited on Apr 14, 2013 at 11:58pm
Apr 15, 2013 at 12:05am
I still don't see why you have lines 2 and 3 before the start of the while loop. Did you try what happens when those two lines are removed?
Last edited on Apr 15, 2013 at 12:05am
Apr 15, 2013 at 12:30am
So this isn't working for a whitespace-delimited input file?
1
2
3
4
5
6
int counter = 0;
while( (inFile >> studentName[counter]) && (inFile >> testScores[counter]) )	
{
    ++counter;	
}
cout << counter << endl; //<-- tels me how many grades were read in. 
Last edited on Apr 15, 2013 at 6:14pm
Apr 15, 2013 at 1:50am
Well yes. Lines 2 and 3 can be removed if you add a line to subtract 1 from the final count and move the counter to the end of the while statement.

1
2
3
4
5
6
7
8
9
10
	counter = 0;
	  while (infile)	
		{ 
			infile >> studentName[counter];
			infile >> testScores[counter];	
                        counter++;			
		}

	cout<<counter -1;
	cout << endl;


Either one produces the correct answer. Your way is probably the best way because it uses less code.
Apr 15, 2013 at 7:49am
I think the suggestion from moorecm is better, not because it uses more or less code, but because the counter is incremented only when the data is successfully read from the file.
Apr 15, 2013 at 8:43am
oh.. Nice I didn't even see moorecms' post. when I responded. I have never seen it done like that. I learn so much from this site. Thank you moorecm.
Apr 15, 2013 at 6:14pm
FYI, I fixed a couple mistakes in my snippet. (Missing parenthesis and the count was off by one.)
Topic archived. No new replies allowed.