3 Replies - 2687 Views - Last Post: 27 November 2010 - 06:05 AM Rate Topic: -----

#1 Joesavage1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 20-July 09

Comparing two text files.

Posted 27 November 2010 - 12:58 AM

Hi,

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




Please Help,

Thanks In Advance,

Joe

This post has been edited by Joesavage1: 27 November 2010 - 01:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Comparing two text files.

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Comparing two text files.

Posted 27 November 2010 - 02:19 AM

Have you tried adding debug messages to confirm that your arrays contain the data which you're expecting them to contain? This may provide clues as to the root of your problem
Was This Post Helpful? 0
  • +
  • -

#3 Joesavage1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 20-July 09

Re: Comparing two text files.

Posted 27 November 2010 - 04:24 AM

View PostBench, on 27 November 2010 - 01:19 AM, said:

Have you tried adding debug messages to confirm that your arrays contain the data which you're expecting them to contain? This may provide clues as to the root of your problem


The arrrays contain the incorrect data that lead to the results of what the user got right and wrong to be incorrect. I tested this using for loops and cout.


Please Help,

Thanks In Advance,

Joe
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Comparing two text files.

Posted 27 November 2010 - 06:05 AM

Describe exactly, and in detail, what is happening and how that is different to what you want.

What is in the input file?
What is the user input?
What does the debug tell you is in the array/s and how is that different to what it should be?
What is the output? How is that different to what it should be?

Tell us the whole story and describe the bug as concisely and accurately as you can. This "The arrrays contain the incorrect data that lead to the results of what the user got right and wrong to be incorrect. I tested this using for loops and cout." is almost incomprehensible and provides nothing like enough meaningful information to help us help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1