![]() |
| | #1 |
| Village id10t Join Date: May 2008
Posts: 57
| 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;
}
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;
}
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;
}
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 | |
| | #2 |
| Novice Join Date: Jul 2009
Posts: 32
| In future:
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 Code: aaaaabbbbbcccccdddddeeeeeaaaaabbbbbcccccdddddeeeee 1234 Johnny B. Goode 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, ' '); This should get you started. |
| msh is offline | |
| | #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:
| |
| Elysia is offline | |
![]() |
| Tags |
| eof, fstream, loop |
| Thread Tools | |
| Display Modes | |
|
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 |