![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
What is this?
if(test[i]!=' ')
{ score=score+0;}if (test [i] != ' ')
{
// There's an answer
if (test [i] == key [i]) score += 2;
else score -= 1;
}
__________________
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 |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
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. |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
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 |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 115
Rep Power: 3
![]() |
Quote:
Thank you B |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |