Eof causes loop

This is a discussion on Eof causes loop within the C++ Programming forums, part of the General Programming Boards category; Hi People, Its been awhile since I worked on C++, but I am back on the horse again. My problem ...

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Exclamation Eof causes loop

    Hi People,

    Its been awhile since I worked on C++, but I am back on the horse again.

    My problem seems to be that EOF causes a loop.

    Example :

    I have a file with 3 lines of data, 50 characters, a space,4 numbers, a space, x amount of characters.

    The first 50 characters is a sort of a multiple choice answers that are compared to 50 characters in another file. The 4 numbers is a students ID, and the last bunch of characters is the students ID.

    Now you should ouput the data to a file like so:

    Student_number Student_Name Mark_obtained

    First I tried this...

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    void WriteReport(ifstream& booklet_file,ifstream& answer_file)
    {
    
    char character_answer,character_booklet,next;
    int count=0,;
    
    
    
    
    
    while(!answer_file.eof())
    {    
        while(next!=' ')
       {
        booklet_file.get(character_booklet);
        if (character_booklet==next)
        count++;
        answer_file.get(next);
       }  
      
       answer_file.get(next);
      
       while(next!=' ')
        {
         cout<<next;
         answer_file.get(next);
        }  
      cout<<" ";
      answer_file.get(next);
      
      while(next!='\n')
      {
       cout<<next;
       answer_file.get(next);
      } 
    
    cout<<" ";
    cout<<count;
    cout<<endl;
    
    } 
    }
    int main()
    {
       
       ifstream booklet_file,answer_file;
       ofstream report_file;
    
       
       booklet_file.open("booklet.dat");
       answer_file.open("answer.dat");
       
       if (booklet_file.fail())
        {
         cout<<"File read error."<<endl;
         exit(1);
        }  
       
       if (answer_file.fail())
        {
          cout<<"File read error."<<endl;
          exit(1);
        }
        
    
      WriteReport(booklet_file,answer_file);
    booklet_file.close();
    answer_file.close();
    report_file.close();
        return 0;
    }
    But this causes a ugly loop where only the last character is displayed over & over again.

    Then I tried
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    void WriteReport(ifstream& booklet_file,ifstream& answer_file)
    {
    
    char character_answer,character_booklet,next;
    int count=0,;
    
    
    
    
    
    while(answer_file>>next)
    {    
        while(next!=' ')
       {
        booklet_file.get(character_booklet);
        if (character_booklet==next)
        count++;
        answer_file.get(next);
       }  
      
       answer_file.get(next);
      
       while(next!=' ')
        {
         cout<<next;
         answer_file.get(next);
        }  
      cout<<" ";
      answer_file.get(next);
      
      while(next!='\n')
      {
       cout<<next;
       answer_file.get(next);
      } 
    
    cout<<" ";
    cout<<count;
    cout<<endl;
    
    } 
    }
    int main()
    {
       
       ifstream booklet_file,answer_file;
       ofstream report_file;
    
       
       booklet_file.open("booklet.dat");
       answer_file.open("answer.dat");
       
       if (booklet_file.fail())
        {
         cout<<"File read error."<<endl;
         exit(1);
        }  
       
       if (answer_file.fail())
        {
          cout<<"File read error."<<endl;
          exit(1);
        }
        
    
      WriteReport(booklet_file,answer_file);
    booklet_file.close();
    answer_file.close();
    report_file.close();
        return 0;
    }
    And again a ugly screen full of characters.

    when I change the function to this
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    
    void WriteReport(ifstream& booklet_file,ifstream& answer_file)
    {
    
    char character_answer,character_booklet,next;
    int count=0,;
    
        while(next!=' ')
       {
        booklet_file.get(character_booklet);
        if (character_booklet==next)
        count++;
        answer_file.get(next);
       }  
      
       answer_file.get(next);
      
       while(next!=' ')
        {
         cout<<next;
         answer_file.get(next);
        }  
      cout<<" ";
      answer_file.get(next);
      
      while(next!='\n')
      {
       cout<<next;
       answer_file.get(next);
      } 
    
    cout<<" ";
    cout<<count;
    cout<<endl;
    
    } 
    
    int main()
    {
       
       ifstream booklet_file,answer_file;
       ofstream report_file;
    
       
       booklet_file.open("booklet.dat");
       answer_file.open("answer.dat");
       
       if (booklet_file.fail())
        {
         cout<<"File read error."<<endl;
         exit(1);
        }  
       
       if (answer_file.fail())
        {
          cout<<"File read error."<<endl;
          exit(1);
        }
        
    
      WriteReport(booklet_file,answer_file);
    booklet_file.close();
    answer_file.close();
    report_file.close();
        return 0;
    }
    The output is much better, but it prints only 1 record, ignoring the remainder of the file.

    I have been through chapter 6 of "Problem Solving with C++" by Savitch and I cant find the reason why EOF causes the loop.

    I haven't been a active programmer for about 6 months now, so please be gentle.

    PS This is a homework assignment, so if you can, I would appreciate tips (not full code)

    Thanks!

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    In future:
    1. Current version of code only - despite the popular opinion, programmers don't like to read the code of others unless it's really interesting (yours is not).
    2. Your indentation is a mess - fix it.
    3. Spaces around operators, and after commas.


    You say we have a files with three lines of data. That would imply a format like this:
    Code:
    aaaaabbbbbcccccdddddeeeeeaaaaabbbbbcccccdddddeeeee // test answers
    1234 // student ID
    Johnny B. Goode //student name
    However, you say you have space between the "lines" which makes me guess, and this is supported by your code, that you have a list of space separated values, e.g.:
    Code:
    aaaaabbbbbcccccdddddeeeeeaaaaabbbbbcccccdddddeeeee 1234 Johnny B. Goode
    WriteReport() is a mess.

    Variable next is not initialized before use. You're approach to reading the test answers string is debatable - it would be easier to read it in whole (into an array) and when do counting and comparison. E.g.:
    Code:
    char answers[51];
    // Read all until we hit the first space or 50 characters are read,
    // see http://www.cplusplus.com/reference/iostream/istream/getline/
    booklet_file.getline(answers, 51, ' ');
    Same for the other elements - student ID and student name. Especially for the name.

    This should get you started.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Don't use eof for loop conditions either - see FAQ.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Tags for this Thread


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