Okay so i'm in a c++ programming class and i need some help. I'm trying to take my file and make it so it compares two char arrays from files that i already have. I'm not looking for an answer just some hints and advice because i have no idea how to do this. My code looks like this.
Line 23,37: Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1 2 3
while (stream >> var) // or while (getline(stream,var))
{ // Good operation
}
Line 46,56: array3 is not needed. Just use i+1.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
thank you for your help but what my code works for everything i needed it to work for before but now i need to make it so that it goes through and checks to see if the correct answers matches the student answers. If it doesn't it needs to display the ones that aren't correct. i'm not sure how to do that.
You simply need to iterate through the arrays comparing the student answers with the correct answers.
1 2 3 4 5 6 7 8 9 10
// Compares the student's answers to the correct answers
// Returns number of correct answers
int evaluate_answers (constchar correct_ans[], constchar student_ans[])
{ int correct = 0;
for (int i=0; i<arraysize; i++)
if (student_ans[i] == correct_ans[i])
correct++;
return correct;
}
BTW, your variable names for your arrays (array1, array2) suck. Your variable names give no useful information to the reader.