Reading From File - Using While Loop

I'm trying to make the following Read from file end when it encounters -1.
however the following code continuous reading the -1 row. Does it have something to do with how i'm wording my expression? score1 != -1? (Data4 below code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}

* Sorry for weird spacing. It pasted weird.
Data4
90 90 90
85 85 85
96 89 45
89 85 95
90 85 97
88 95 90
-1 00 10
50 56 89
I'd change your program a bit, so that you also check the stream state. Something like:

1
2
3
4
5
6
7
8
9
10
11
   while(inputStream >> score1 >> score2 >> score3 && score1 != -1){
      averageTestScore = (score1 + score2 + score3) / 3;

      cout << "Student # " << ++counter << "'s average is: "  << averageTestScore << endl;

      totalAverage = totalAverage + averageTestScore;
   }
   cout << "Total class average is: " << (totalAverage / counter);

   return 0;
}


Also doyoualwayswriteyoursentenceslikethis? Notice the added whitespace in the above snippet. And since all of your variables are integral values I hope you're aware that integral math has no fractional parts.
I appreciate the lesson on white space as c++ is a program that allows me to use as much of that as i want. However the above change in while didn't help much. It actually read less of the Data4 file if anything. I am interested in what you said about integral math having no fractional parts. Do you mean remainders? If i am unaware of something in c++ i would like to be informed. :D thank you!
If it reads nothing your file could not be opened. Do you know where the program is searching for the file? If not use for now an absolute path.
Do you mean remainders?

Yes.

However the above change in while didn't help much. It actually read less of the Data4 file if anything.


Are you sure the file actually opened correctly?

The program seems to work for me:
Student # 1's average is: 90
Student # 2's average is: 85
Student # 3's average is: 76
Student # 4's average is: 89
Student # 5's average is: 90
Student # 6's average is: 91
Total class average is: 86


I wasn't sure! Thanks. I'm currently modifying it trying to understand how and why this worked. I kept the redundant inputStream

while(inputStream >> score1 >> score2 >> score3 && score1 != -1){
inputStream>>score1>>score2>>score3; (atleast i think this is redundant)

also regarding the remainders i could just used double. Unless i'm wrong. However i do appreciate your help jlb!
inputStream>>score1>>score2>>score3; (atleast i think this is redundant)
Not really redundant, but it is incorrect. By having two read statements you will skip ever other line.

Remember in the while loop you are reading the three scores, then testing the stream state for errors. If there are errors reading any of the variables the loop will end because of the failed stream. Otherwise it will check to see if score is not equal to negative one. If it is equal to negative one then the loop will terminate.

also regarding the remainders i could just used double.

If you want your averages to show fractional parts then yes double would be a good choice. And remember when doing calculations using constants at least one side of the equation must be a floating point number to force floating point math.

3 / 4 * 5.0
The above calculation will yield zero because 3 / 4 uses integer math and the result yields zero.

And please please start using whitespace to help make your code readable. Lumping everything together makes it harder for humans to read the code.



Last edited on
Topic archived. No new replies allowed.