Help with reading from files and calculating average

l
Last edited on
Is there a problem with your code, or do you have some specific question?

My code works very fine with the first student and every time I add while loop to read rest of the students it just doesn't work.
I added while(!fin.eof()) but it went into infinite loop with the first student
Why doesn't it work? Ask specific questions or tell us exactly what is going wrong.

You have several possible problems.

1. Where are you extracting that "sentinel" value?

2. Since you're switching between the extraction operator>> and getline you can expect problems caused by the left over newline character.

3. This else if (80 < average <= 90) will not produce the desired effect. You can't combine "tests" like this in C++ you need to use one of the logical operators( &&, ||, etc) and you must have both sides of the comparison (average > 80).



The specific question is how do i read line by line from the files using loop.
Did you design the input file or was it given to you by your instructor?

Given by instructor... I can email the assignment paper to you bcuz its really hard to ask specific question when i dont know what the problem is....
There are several probable problems.

First I don't see where you're extracting that sentinel character. You probably should be using a while() loop to extract the grades instead of the for loop.
1
2
3
4
while(fin >> score && score != -1)
{
    // Your code here.
}

Second when switching between the extraction operator>> and getline() you need to be sure the end of line character is properly handled after the extraction operator but before getline(). One quick way is to remove the leading whitespace characters in the getline call:

getline(fin >> ws, name);

Topic archived. No new replies allowed.