Reading from file - Using While Loop

I am having trouble with the following code. I need it to end when it reaches -1 however it reads that row and then ends. Does it have something to do with my statement? score1 != -1?

#include <iostream>
#include <fstream>

using namespace std;

int main(){
fstream inputStream;

int score1, score2, score3, averageTestScore, totalAverage=0, counter=0;

inputStream.open("Data4.txt");

while(score1 != -1){
counter++;
inputStream >> score1 >> score2 >> score3;
averageTestScore=(score1+score2+score3)/3;
cout<<"Student # "<<counter<<"'s average is: "<<averageTestScore<<endl;
totalAverage = totalAverage + averageTestScore;
}
cout<<"Total class average is: "<<(totalAverage/counter);

inputStream.close();
return 0;
}

Data4.txt
90 90 90
85 85 85
96 89 45
89 85 95
90 85 97
88 95 90
-1 00 10
50 56 89

The easiest fix will probably be to add an if statement after you read the data using inputStream that checks if score1 is -1 and throws out the data if it is.
I appreciate your help Tresky!
Another user helped me solve this.

below is a solution i will possibly use

while(inputStream >> score1 >> score2 >> score3 && score1 != -1){
averageTestScore=(score1+score2+score3)/3;
cout<<"Student # "<<counter++<<"'s average is: "<<averageTestScore<<endl;
totalAverage=totalAverage+averageTestScore;
Glad to hear you fixed it. In the future, please refrain from posting to two separate forum sections (Beginners and General C++). One should suffice. :)
Topic archived. No new replies allowed.