Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 23rd, 2006, 9:40 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
test score

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
brad sue is offline   Reply With Quote
Old Sep 23rd, 2006, 10:09 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
What is this?
                  if(test[i]!=' ')  
                      { score=score+0;}
If you don't do anything to the score, it's the same as adding zero. I would write it like this:
if (test [i]  != ' ')
{
   // There's an answer
   if (test [i] == key [i]) score += 2;
   else score -= 1;
}
Secondly, it would be better to use the string class. I would read the entire line and pluck the two parts out as substrings.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 24th, 2006, 10:00 AM   #3
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by DaWei View Post
What is this?
                  if(test[i]!=' ')  
                      { score=score+0;}
If you don't do anything to the score, it's the same as adding zero. I would write it like this:
if (test [i]  != ' ')
{
   // There's an answer
   if (test [i] == key [i]) score += 2;
   else score -= 1;
}
Secondly, it would be better to use the string class. I would read the entire line and pluck the two parts out as substrings.
Thank you Dawei.
beside the silly
score=score+0;
I often mess up what I want to compare to. ( like key [i]==test [i] instead of test [i] == key [i])
I think we are going to look at class string next week. We'll see if I can change it as we go along.
brad sue is offline   Reply With Quote
Old Sep 24th, 2006, 10:54 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
We all do silly stuff, Brad. I haven't done it in a long time, of course. Yesterday, I think. Hang in there.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 26th, 2006, 11:01 PM   #5
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
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
Old Sep 27th, 2006, 5:40 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Sure, EOF is the issue. Your last read grabs the last line and newline. That is not EOF, since you haven't attempted to read beyond it. The "while (fin)" looks fine, so you attempt to read again and hit EOF. You aren't checking for that. This is one of most common novice mistakes.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 3rd, 2006, 12:03 AM   #7
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Sure, EOF is the issue. Your last read grabs the last line and newline. That is not EOF, since you haven't attempted to read beyond it. The "while (fin)" looks fine, so you attempt to read again and hit EOF. You aren't checking for that. This is one of most common novice mistakes.
Dawei, can I have some clarifications about what you are saying because I am not able to see what to modify.
Thank you
B
brad sue is offline   Reply With Quote
Old Oct 3rd, 2006, 7:37 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Presume that this is the last thing in your file: abcdefg\n(EOF). The input mechanism is satisfied when it reads the \n. It is sitting between that and the EOF. You use that, then return to the top of the while, which is NOT showing EOF. You read and immediately hit EOF. Since you aren't looking to see if that's the case, you read for a name, some chars, and some scores, each of which does absolutely nothing, since the stream failed on the try for a name and has been blithely ignoring you. Didn't keep you from trying to use the stuff you already had, though. Look at each individual read mechanism you use. Ask what happens if it hits EOF before getting anything. If you don't, you'll reuse crap. Ask, also, what happens if it hits EOF AFTER getting some stuff. If you don't recognize THAT, you'll throw good stuff away. It is a common newbie mistake to rely on EOF or FAIL in the while expression, only. Sometimes that's workable, mostly not.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
test lostcauz Coder's Corner Lounge 9 Jul 18th, 2006 3:20 PM
Sorting taporctv Java 9 Apr 15th, 2006 8:55 AM
[Python] BlackJack UnKnown X Show Off Your Open Source Projects 9 Feb 20th, 2006 6:01 AM
Advanced Python Tricks Arevos Python 19 Sep 24th, 2005 7:39 AM
Separating Axis Test. Desperate for help xennon C++ 0 Apr 24th, 2005 7:08 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC