File I/O question

I am studying file I/O and writing simple programs. This one has an issue I don't understand and any enlightenment would be greatly appreciated.

Here is the 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	// declare variables
	int score, totalScore = 0, count = 0; // count is count of students(or scores)
	int numA = 0, numB = 0, numC = 0, numD = 0, numU = 0; // number of As, ..., etc
	ifstream input;
	ofstream output;

	// open file scores.dat for read. 
	// if open file for read fails, print out error message and exit the program (see demo for details)
	input.open("scores.dat");

	if(input.fail())
	{
		cout << "Unable to open file scores.dat to read";
		system("pause");
		exit(1);
	}

	// as long as there is a score in file to read
	while(input >> score)
	{
		// read a score from 	
		input >> score;
		
		// update total score
		totalScore = totalScore + score;

		// update the number of As, Bs, Cs, Ds, and Us
		switch(score/10)
		{
		case 10: case 9:
			numA++;
			break;
		case 8:
			numB++;
			break;
		case 7:
			numC++;
		case 6:
			numD++;
		default:
			numU++;
			break;
		}

	// update the count of students
	count++;
	}
	


	// if count of students is 0, print out error message and exit program
	if(count == 0)
	{
		cout <<  "Student count is 0. Program exit.";

	}

	else
	{
		// open file YourName.txt for write
		output.open("MounesJeremy.txt");

		// set correct ios flags 
		output.setf(ios::fixed);
		output.setf(ios::showpoint);
		output.precision(2);

		// output result to the file
		output << "\n\n\t\tTest Statistics";
		output << "\n\n\tTotal number of students: " << count;
		output << "\n\tNumber of A's: " << numA;
		output << "\n\tNumber of B's: " << numB;
		output << "\n\tNumber of C's: " << numC;
		output << "\n\tNumber of D's: " << numD;
		output << "\n\tNumber of U's: " << numU;
	}

	cout << "\n\tThe test stats were output to MounesJeremy.txt";
	cout << "\n\tThank you for using MyTeachingAssistant software\n\t";
	
	// close files
	input.close();
	system("pause");
	return 0;
}


The problem is, while this method seems to work, when a list of 26 integers is given:

1
2
3
4
5
6
7
8
		Test Statistics

	Total number of students: 13
	Number of A's: 4
	Number of B's: 3
	Number of C's: 2
	Number of D's: 5
	Number of U's: 6 


It reads exactly half before stopping.

Why?
Here you're doing two reads (one within the while statement and you read the next one right after):

1
2
3
4
while(input >> score)
{
    // read a score from 	
    input >> score;
Thanks.

That should have been obvious, but the use of an input/output as a while loop exit condition was confusing to me.
What's happening there is it does the read and then input itself is used as the condition. Using an instance of ifstream or ofstream as a condition is basically the same thing as saying myStreamInstance.good().
I wasn't aware the declaration of a do/while loop allowed for operations of any kind. I thought while loops were conditional only, with for loops reserved for that type of thing.

Would this work, then?

while(i++)
{
do something

if(i > 5) { break; }
}

Mark as solved, yes I am awar of the dumb stuff in that program, such as the missing break;'s
Last edited on
Topic archived. No new replies allowed.