I've recently been given an assignment to compare two text files - both of which have answers to a 20 question exam in.
So I came up with some code, but for some unknown reason the arrays that I store the values in don't hold the same answers as the users put in the text files..
Here is my code:
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int main(){
char Answer;
char StudentAnswer;
int x = 0; //Counting variable
char Answers[20];
char StudentAnswers[20];
int Score = 0;
ifstream AnswersTXT("Answers.txt");
if(AnswersTXT.is_open()){
while(!AnswersTXT.eof()){
AnswersTXT.get(Answer);
Answers[x] = Answer;
x++;
}
AnswersTXT.close();
} else {
cout << "File could not be found." << endl;
}
x = 0; //Reset the counting variable
ifstream StudentAnswersTXT("MyAnswers.txt");
if(StudentAnswersTXT.is_open()){
while(!StudentAnswersTXT.eof()){
StudentAnswersTXT.get(StudentAnswer);
StudentAnswers[x] = StudentAnswer;
x++;
}
AnswersTXT.close();
} else {
cout << "File could not be found." << endl;
}
for(x=0;x<20;++x){
if(StudentAnswers[x]==Answers[x]){
Score++;
cout << "You were correct on question " << x+1 << "!" << endl;
} else {
cout << "You were incorrect on question " << x+1 << ". You put " << StudentAnswers[x] << " but the answer was " << Answers[x] << endl;
}
}
getch();
return 0;
}
The program compiles correctly and everything, but when it talks about you being correct or incorrect - it seems to lie and think that you put different answers than you actually did.
The TXT files are both formatted like this:
Quote
A
B
C
D
A
B
C
B
C
D
A
B
C
Please Help,
Thanks In Advance,
Joe
This post has been edited by Joesavage1: 27 November 2010 - 01:00 AM

New Topic/Question
Reply



MultiQuote






|