| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Loop problem
Hi ,
I am coding a 'test corrector' machine. I need to read a name from a file . The student name is delimitated by the '#' sign . After the name is a space and then the response to the test in form of T (true)and F(false). I want to read the name of the student, followed by his answer, followed by the test score. Each correct answer counts 2 points, each wrong answer -1 point, and no answer 0 point. My problem is my loop reads 1 more line and prints garbage. for example, the file is like this: TFFTFFTTTTFFTFTFTFTF // test key John Doe# TFTFTFTT TFTFTFFTTFT // note the blank in the response! Chis Fro# TFTTTFFTTFFTTTFTTFFT my output should be: John Doe TFTFTFTT TFTFTFFTTFT score=8 Chris Fro TFTTTFFTTFFTTTFTTFFT score=9 I have something weird with my output. It is like the program reads one more line and compute a score . It is like this: John Doe TFTFTFTT TFTFTFFTTFT score=8 Chris Fro TFTTTFFTTFFTTTFTTFFT score=9 --------(this is all blank)----------score=13// this the 'invisible line' I don't know why it is doing this. I checked my program and I dont see where it should read one more line. I know that the problem comes from the EOF but I cannot fix it. I don't know why my score is wrong. Here is my code: Code:
#include<fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE=100;
int main()
{
ifstream fin;
ofstream fout;
fin.open("input.txt");
if(fin.fail())
{
cerr<<" Input file opening failed.\n";
exit(1);
}
fout.open("out1.txt");
if(fout.fail())
{
cerr<<" output file opening failed.\n";
exit(1);
}
char name[SIZE]={0};
char key[20];// key of the test
char test[20];// response of student
char next;// char used to adjust see below
// read key
for(int i=0;i<20;i++)
{
fin>>key[i];
}
// display key
for(int i=0;i<20;i++)
{
cout<<key[i];
}
cout<<endl;
int index=0;
int point=0;
static int score=0;
while( fin)
{
fin.get(name,SIZE,'#');
cout << name << endl;
point=strlen(name);// check to kow where the file cursor is!
fin.get(next);// adjust beginning student response array
fin.get(next);
fin.get(test,100,'\n');
cout<<test;
for(int i=0;i<20;i++)// score calculation
{
//code to compute score
}// end for
cout<<" "<<score;
}
fin.close();
fout.close();
return 0;
}
|
|
#2
|
|||
|
|||
|
I think this has somethign to do with the fact that the error bit in ifstream is only set after input has failed. So it might be OK, and you enter the loop for the last time and read a newline or something, and then an "invisible line" appears. This might help (note that feof() is similar to ifstream::eof() ).
|
|
#3
|
|||
|
|||
|
Quote:
thank you Ubergeek. I see what you are saying but can find a way to code it. I believe I need to have a "check" after each line. and I dont know how I can use feof() as you suggested me to fix my problem. ![]() |
|
#4
|
|||
|
|||
|
I think you need to put a while loop when you read each line. Something like that:
Code:
while (!fin.eof())
{
fin.get(test, 100, '\n');
cout<<test<<endl;
}
Hope I helped! Costas |
|
#5
|
|||
|
|||
|
Hey, I tried and it doesn't work. Try it though! Sorry I can't help, but file I/O isn't my best part.
|
|
#6
|
|||
|
|||
|
Quote:
Yes, I tried this too. Thanks for help though! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Loop problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|