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!