Thread: test score
View Single Post
Old Sep 26th, 2006, 11:01 PM   #5
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by brad sue View Post
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; }
                      
                  else {score=score-1;}    
         
              }// end for

              cout<<"  score="<<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
I have something weird with my output.
It is like the program reads one more line and compute a a score .

It is like this:

jose frits TTFTTTFFFTFTTFFTTF score=23
deinse guile FTTTTFTFTTFFFTTFT score=19
--------(this is all blank)----------score=13// this the 'invisible line'


I don't knoe why it is doing this. I checked my program and nothing indicates that it should read one more line and I don't think the end of file is the issue.
please can someone see my problem?

Thank you
B
brad sue is offline   Reply With Quote