Thread: what is wrong with eof?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Question what is wrong with eof?

    Write a program to compute numeric grades for a course.Read in a data file which contains the first name of the student and then ten quiz scores in each line.Then compute the ave score for the ten scores and write the result in to another file.

    This my code and my out put.
    Code:
    
    #include <iostream>
     #include <fstream>
     #include <stdlib.h>
     #include <iomanip>
     using namespace std;
    
     void welcome()
     {
          cout<<"This program will compute numeric grades for a course.\n";
          cout<<"After reading in a input file,I will compute the average score\n"
              <<"for each student and write them to a new file.\n";
    
     }
    
     void InputAndOutput(ifstream& in,char name[],int score[],ofstream& out)
     {
          in.open("E:\\fall 2007\\CS 3A\\6.9\\list.txt");
          if(in.fail())
          {
            cout<<"list.txt doesn't exist.Program terminates!\n";
            system("pause");
            exit(1);
          }
    
          out.open("E:\\fall 2007\\CS 3A\\6.9\\score.txt");
          if(out.fail())
          {
            cout<<"score.txt doesn't exist.Program terminates!\n";
            system("pause");
            exit(1);
          }
    
    
          int count=0;
          double sum[100];
    
    
          while(!in.eof())
          {
    
             in>>name>>score[0]>>score[1]>>score[2]>>score[3]>>score[4]>>score[5]
               >>score[6]>>score[7]>>score[8]>>score[9];
    
              cout<<name<<score[0]<<score[1]<<score[2]<<score[3]<<score[4]<<score[5]
               <<score[6]<<score[7]<<score[8]<<score[9];
    
             for(int i=0;i<10;i++)
             sum[count]+=score[i];
    
    
             out<<setw(10)<<name<<setw(5)<<score[0]<<setw(5)<<score[1]<<setw(5)
                <<score[2]<<setw(5)<<score[3]<<setw(5)<<score[4]<<setw(5)<<score[5]
                <<setw(5)<<score[6]<<setw(5)<<score[7]<<setw(5)<<score[8]<<setw(5)
                <<score[9]<<setw(5)<<sum[count]/10.0<<endl;
    
             count++;
    
           }
    
           cout<<"End of the program!\n";
    
     }
    
    
    
    
    
    
     int main()
     {
    
         ifstream in;
         ofstream out;
         char name[10];
         int score[10];
    
    
         welcome();
         InputAndOutput(in,name,score,out);
         in.close();
         out.close();
    
         system("pause");
    
      }

    output


    Tian 98 99 100 99 97 96 99 99 100 99 98.6
    Paul 88 89 90 91 94 93 88 78 99 100 91
    Zerg 99 91 92 78 99 95 94 96 99 100 94.3
    Mengle 99 93 94 92 94 95 99 100 99 100 96.5
    Mengle 99 93 94 92 94 95 99 100 99 100 96.5





    look, the last person do 2 times.So why it happened?I think there is must be something wrong in eof!And another question how do the system know the end of a file?how did the system detect it?

    And also maybe there is some way to simplify my code,so please help!
    Last,I want to enhance my program like:
    If there is more than 10 quiz score,how to make it possible to make my program to handle it?(use a array?)



    thanks!
    Last edited by tx1988; 09-10-2007 at 01:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  2. Anything wrong with this(char to short and int)
    By hckr83 in forum C Programming
    Replies: 3
    Last Post: 12-22-2005, 06:27 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. Dividing and EOF
    By Tride in forum C Programming
    Replies: 7
    Last Post: 09-27-2003, 05:26 PM
  5. Strange EOF happenings
    By fatinez in forum C Programming
    Replies: 6
    Last Post: 09-23-2003, 08:37 PM