HELP!!



Hello. I am new to C++ and i am stuck on a problem. The program I have continues to loop over and over and I can't seem to figure out why. If anyone can help I would greatly appreciate it. Here is what I have:

#include <iostream>
#include <iomanip>
#include<fstream>
using namespace std;
int main()
{
int grade, kount, average, max_score, min_score;
kount = 0;
ifstream inFile;
inFile.open("i:\\programming\\grades.txt");
inFile >> grade;

while (!inFile.eof()) {
if (grade >= 0 && grade <= 100)
kount += 1;

cout << "\n" << setw(10) << grade;

if (grade < 0 || grade > 100)
cout << " Invalid \n";

if (grade >= 0 && grade <= 59)
cout << " Unsatisfactory \n";

if (grade >= 60 && grade <= 89)
cout << " Satisfactory \n";

if (grade >= 90 && grade <= 100)
cout << " Outstanding \n\n";

else
cout << endl;
inFile >> grade;
}

inFile.close();
cout << "\n\n\n Hit enter to continue...";
cin.get();

return 0;
}

Here is the assigment and the output:

Write a program to read in a collection of exam scores ranging in value from 0 to 100. Your program should sort the scores in ascending order and display them in a column. Each score should be followed by a word that indicates its category, such as "Invalid" or "Satisfactory". The program must also count and display the number of outstanding scores (90 - 100), the number of satisfactory scores (60 - 89), the number of unsatisfactory scores (0 - 59) and the number of invalid scores (less than 0 or greater than 100). It should also display the average score (with 2 decimal digits), the highest score, the lowest score, and the category of every score. Do not include the invalid scores in the average, max, or min. You will need to create a data file named grades.txt. (Use notepad to enter the following data values and save them on your drive).


63 75 121 72 72 78 67 -9 80 63 75 90 89 43
59 93 82 -113 33 97 202 40 60 55 88 91 101 66

Your program should print output starting at the top of the screen with your name followed by some column headings to identify the output. Be sure to include remarks identifying the author, date written, and what the program does.

SAMPLE OUTPUT:

Joe Student

Score | Category
----------+---------------
-113 | Invalid
-9 | Invalid
33 | Unsatisfactory
40 | Unsatisfactory
43 | Unsatisfactory
55 | Unsatisfactory
59 | Unsatisfactory
60 | Satisfactory
63 | Satisfactory
63 | Satisfactory
66 | Satisfactory
67 | Satisfactory
72 | Satisfactory
72 | Satisfactory
75 | Satisfactory
75 | Satisfactory
78 | Satisfactory
80 | Satisfactory
82 | Satisfactory
88 | Satisfactory
89 | Satisfactory
90 | Outstanding
91 | Outstanding
93 | Outstanding
97 | Outstanding
101 | Invalid
121 | Invalid
202 | Invalid

Statistics:

28 | Scores read from file
4 | Outstanding Scores (90-100)
14 | Satisfactory Scores (60-89)
5 | Unsatisfactory Scores (0-59)
5 | Invalid Scores (Under 0 or Over 100)
70.91 | Average Score
97 | Highest Score
33 | Lowest Score

Any input would be greatly appreciated!!

closed account (48T7M4Gy)
Read this short tutorial and use the demo program for reading text files as a pattern.

You can easily get this demo to read your file.

Do this before you go on and process the data because that just adds complexity and makes the bug difficult to find.

Your while loop is probably the cause. Use a cut down version of your program to get that part right before you cut and paste the rest in. :)

http://www.cplusplus.com/doc/tutorial/files/
Most likely the file was not opened successfully.
After the open() statement, you go on to read from the file without checking it was actually open.

Also, it is not recommended to loop on eof().
An alternative would be to replace
while (!inFile.eof())
with
while (inFile)
Topic archived. No new replies allowed.