I need help with the eof loop

This is a discussion on I need help with the eof loop within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm doing an assignment that requires the program to read data which consists of names and grade scores with ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    I need help with the eof loop

    Hello.
    I'm doing an assignment that requires the program to read data which consists of names and grade scores with each scores ending with a negative number from an input file.
    It figures out an average and posts a grade for each person. The only problem that I'm having is that after going through the first name and score, the following names either come out fine or as numbers and the scores don't come out completely and it's just messy and the average always displays the average from the first reading. I tried to solve this by setting the score, average, etc = 0. Now all it does is display 0s infinitely and I have no idea how to fix it.
    Any help is appreciated.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    #include<fstream>
    using namespace std;
    
    void main()
    
    {
        double score = double();
        double sum = double ();
        int count = int();
        double average = double();
        string fname = string();
        string lname = string();
    
        ifstream in;
        in.open("E:\\in.txt");
    
        cout << setprecision(3) << endl;
    
        while(!in.eof())
        {
            in >> fname >> lname;
            cout << "Your name is: " << fname << " " << lname << endl;
            cout << endl;
            cout << "Your scores are: ";
    
            while(score>=0)
            {
                in >> score;
                cout << score << " ";
               
                if(score>=0)
                {
                    sum = sum + score;
                    count++;
                }
    
                else
                    if(count<=1)
                {
                    cout << endl << endl;
                    cout << fname << " " << lname << ", you didn't enter any scores." << endl;
                    cout << endl;
                }
            }
    
            average = (sum / count);
    
            if((average >= 0) && (average <= 55))
            {
                cout << endl << endl;
                cout << fname << " " << lname << ", your average is " << average << " and your letter grade is an F." << endl;
                cout << endl << endl;
            }
    
            else
                if((average > 55) && (average <= 67.5))
                {
                    cout << endl << endl;
                    cout << fname << " " << lname << ", your average is " << average << " and your letter grade is a D." << endl;
                    cout << endl << endl;
                }
    
            else
                if((average > 67.5) && (average <= 80))
                {
                    cout << endl << endl;
                    cout << fname << " " << lname << ", your average is " << average << " and your letter grade is a C." << endl;
                    cout << endl << endl;
                }
    
            else
                if((average > 80) && (average <= 90))
                {
                    cout << endl << endl;
                    cout << fname << " " << lname << ", your average is " << average << " and your letter grade is a B." << endl;
                    cout << endl << endl;
                }
    
            else
                if(average > 90)
                {
                    cout << endl << endl;
                    cout << fname << " " << lname << ", your average is " << average << " and your letter grade is an A." << endl;
                    cout << endl << endl;
                }
    
            sum = 0;
            count = 0;
            average = 0;
            score =0;
        }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    while(!in.eof())

    Do not use eof() to control loop - read FAQ
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21