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 that my score is not right.
for example, the file is like this:
TFFTFFTTTTFFTFTFTFTF // test key
John Doe# TFTFTFTT TFTFTFFTTFT // note the blank in the response!
my output should be:
John Doe TFTFTFTT TFTFTFFTTFT score=8 (I think ! but I found 22!!)
I don't know why my score is wrong. Here is my 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
{
if(key[i]==test[i])
{ score=score+2; }
if(test[i]!=' ')
{ score=score+0;}
else {score=score-1;}
}// end for
cout<<" "<<score;
}
fin.close();
fout.close();
return 0;
}
please can someone help me .
I first had some difficulty to read the name and then the response, and I dont know if I 've done it correctly
Thank you
B