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);
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.
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. :)