![]() |
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>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 |
What is this?
:
if(test[i]!=' ') :
if (test [i] != ' ') |
Quote:
beside the silly :
score=score+0;I think we are going to look at class string next week. We'll see if I can change it as we go along. |
We all do silly stuff, Brad. I haven't done it in a long time, of course. Yesterday, I think. Hang in there.
|
Quote:
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 |
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.
|
Quote:
Thank you B |
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.
|
| All times are GMT -5. The time now is 11:23 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC