C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-06-2009, 01:37 PM   #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!
MarlonDean is offline   Reply With Quote
Old 08-07-2009, 02:11 AM   #2
msh
Novice
 
Join Date: Jul 2009
Posts: 32
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.
msh is offline   Reply With Quote
Old 08-07-2009, 03:39 AM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Don't use eof for loop conditions either - see FAQ.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
eof, fstream, loop

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
EOF or not EOF? CornedBee Linux Programming 2 09-14-2007 02:25 PM
Personal Program that is making me go wtf? Submeg C Programming 20 06-27-2006 12:13 AM
A somewhat bizzare problem!!! - WHILE LOOP bobthebullet990 C Programming 3 03-31-2006 07:19 AM
for loop or while loop slamit93 C++ Programming 3 05-07-2002 04:13 AM
error in do-while loop correctly handling ( ) in arithmetic expressions Unregistered C Programming 1 11-19-2001 04:45 PM


All times are GMT -6. The time now is 05:15 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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