13 Replies - 1539 Views - Last Post: 17 July 2011 - 10:13 PM Rate Topic: -----

#1 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 07:16 PM

Hey this is my first time using this site and I am a beginner so i'm sorry if this seems like a dumb question. I am trying to compare 2 .txt files that have been inputted as arrays and find the differences to come up with an overall grade and questions missed. I can't seem to find the problem though when I debug my code i receive this error message: "Run-Time Check Failure #2 - Stack around the variable 'StudentAnswers' was corrupted." I've tried searching the internet for these problems but i can't seem to find the problem inside my code. Here's my code any help would be greatly appreciated!!

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	char answers;
	char Correct;
	char Student;
	char CorrectAnswers[21];
	char StudentAnswers[21];
	int CorrectCount = 0;
	int StudentCount = 0;
	int count = 0;
	int score = 0;
	int missed;
	int grade;
	
	ifstream fin;
	ifstream fin2;
	fin.open ("CorrectAnswers.txt");
	while(fin.get(answers))
	{
			CorrectAnswers[CorrectCount++] = answers;
			CorrectAnswers[CorrectCount] = 0;
	}
	
	fin2.open("StudentAnswers.txt");
	while(fin2.get(answers))
	{
			StudentAnswers[StudentCount++] = answers;
			StudentAnswers[StudentCount] = 0;
	}

	for(count = 0; count < 21; count++)
	{
		if(CorrectAnswers[count] = StudentAnswers[count])
		{
			score++;
		}
		else
		{
			cout << "You were wrong on " << count + 1 << ". Your answer was " << StudentAnswers[count] <<
			" but the correct answer was " << CorrectAnswers[count] << ".\n\n";
		}
	}

	missed = 20 - score;
	cout << "The total number of questions missed were " << missed <<".\n\n";

	grade = (score / count);
	cout << "Your percentage on the exam is " << grade << ".\n\n";

	if (grade >= .70)
	{
		cout << "You have passed the exam!" << endl;
	}
	else
	{
		cout << "You have failed the exam." << endl;
	}
	fin.close();
	return 0;
}



edit by ishkabible: please use code tags when posting code. like so, :code: thank you :)

The text files i am trying to compare are supposed to have only 20 answers in them in this style:

A
B
C
D
A

This post has been edited by ishkabible: 17 July 2011 - 07:25 PM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Comparing 2 Arrays, Run-Time Check Failure#2

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 07:29 PM

it means you went out of bounds of the array somewhere. what line dose the debugger point to when it gives that error?
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 07:31 PM

The error is most likely due to reading/writing to invalid memory. This could be due to having too many characters in your file and causing the stack around your array to be corrupt.

I have a question about the logic here
while(fin.get(answers))
{
CorrectAnswers[CorrectCount++] = answers; //line 1
CorrectAnswers[CorrectCount] = 0; //line 2
}



Can you tell me what the value of CorrectCount is on line 1 and line 2?
Was This Post Helpful? 0
  • +
  • -

#4 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 07:38 PM

View Postishkabible, on 17 July 2011 - 07:29 PM, said:

it means you went out of bounds of the array somewhere. what line dose the debugger point to when it gives that error?

the debugger points to the last line of the code which is just the final bracket.

View PostImaSexy, on 17 July 2011 - 07:31 PM, said:

The error is most likely due to reading/writing to invalid memory. This could be due to having too many characters in your file and causing the stack around your array to be corrupt.

I have a question about the logic here
while(fin.get(answers))
{
CorrectAnswers[CorrectCount++] = answers; //line 1
CorrectAnswers[CorrectCount] = 0; //line 2
}



Can you tell me what the value of CorrectCount is on line 1 and line 2?


For line one the CorrectCount i'm just trying to count the answers to compare them later on but the second line it should be 20.
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 07:55 PM

the ++ operator permanently increments your variable. Are you purposely setting the values in your array to zero?
Was This Post Helpful? 0
  • +
  • -

#6 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 08:06 PM

View PostImaSexy, on 17 July 2011 - 07:55 PM, said:

the ++ operator permanently increments your variable. Are you purposely setting the values in your array to zero?


No im trying to get the count of the array to start at zero and then count up to 19. so i can then compare the 2 files and see which letters of each file match and don't match up.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 08:41 PM

First since you are using arrays to hold your CorrectAnswers and StudentAnswers you and you are using a while to read the data from the file you should always check to insure the index value (CorrectCount) is less than the array size. This is to prevent possible access past the array bounds. You should stop processing the file if it has more than the expected number of entries.

Second: In the following snippet:
while(fin.get(answers))
{
   CorrectAnswers[CorrectCount++] = answers; //line 1
   CorrectAnswers[CorrectCount] = 0; //line 2
}


You do not need the second line. IF CorrectCount is 20 at the beginning of the loop when you get to this point your index will be 21 which is larger than the size of your array.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 08:57 PM

Thank you Jim. That was helpful, but when i corrected my code to delete line 2 and change the size to 20, i am now having trouble with the code not giving me the correct amount of numbers missed here's my code now:

 #include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	char answers;
	char CorrectAnswers[21];
	char StudentAnswers[21];
	int CorrectCount = 0;
	int StudentCount = 0;
	int count = 0;
	int score = 0;
	int missed = 0;
	int grade = 0;
	
	ifstream fin;
	ifstream fin2;
	fin.open ("CorrectAnswers.txt");
	while(fin.get(answers))
	{
			CorrectAnswers[20] = answers;
	}
	
	fin2.open("StudentAnswers.txt");
	while(fin2.get(answers))
	{
			StudentAnswers[20] = answers;
	}

	for(count = 1; count < 21; count++)
	{
		if(CorrectAnswers[count] = StudentAnswers[count])
		{
			score++;
		}
		else
		{
			cout << "You were wrong on " << count + 1 << ". Your answer was " << StudentAnswers[count] <<
			" but the correct answer was " << CorrectAnswers[count] << ".\n\n";
		}
	}
	
	missed = 20 - score;
	cout << "The total number of questions missed were " << missed <<".\n\n";

	grade = (score / count);
	cout << "Your percentage on the exam is " << grade << ".\n\n";

	if (grade >= .70)
	{
		cout << "You have passed the exam!" << endl;
	}
	else
	{
		cout << "You have failed the exam." << endl;
	}
	fin.close();
	return 0;
}


This post has been edited by sharkman: 17 July 2011 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 09:23 PM

This CorrectAnswers[20] will always write to element number 21. Is that what you want? I don't think so. Using the index variable was probably what you wanted but you need to insure that you never try to access the array with an index larger than the array size.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 09:38 PM

View Postjimblumberg, on 17 July 2011 - 09:23 PM, said:

This CorrectAnswers[20] will always write to element number 21. Is that what you want? I don't think so. Using the index variable was probably what you wanted but you need to insure that you never try to access the array with an index larger than the array size.

Jim


Thank you i finally got the run time failure to stop, but now i think i'm just missing something simple with the rest of the code.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 09:41 PM

Post your modified code and tell us the problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 09:53 PM

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	char answers;
	const int size = 21;
	char CorrectAnswers[size];
	char StudentAnswers[size];
	int CorrectCount = 0;
	int StudentCount = 0;
	int count = 0;
	int score = 0;
	int missed = 0;
	int grade = 0;
	
	ifstream fin;
	ifstream fin2;
	fin.open ("CorrectAnswers.txt");
	while(fin.get(answers))
	{
			CorrectAnswers[CorrectCount++] = answers;
	}
	
	fin2.open("StudentAnswers.txt");
	while(fin2.get(answers))
	{
			StudentAnswers[StudentCount++] = answers;
	}

	for(count = 0; count < 20; count++)
	{
		if(CorrectAnswers[count] = StudentAnswers[count])
		{
			score++;
		}
		else
		{
			cout << "You were wrong on " << count + 1 << ". Your answer was " << StudentAnswers[count] <<
			" but the correct answer was " << CorrectAnswers[count] << ".\n\n";
		}
	}
	
	missed = 20 - score;
	cout << "The total number of questions missed were " << missed <<".\n\n";

	grade = (score / count);
	cout << "Your percentage on the exam is " << grade << ".\n\n";

	if (grade >= .70)
	{
		cout << "You have passed the exam!" << endl;
	}
	else
	{
		cout << "You have failed the exam." << endl;
	}
	fin.close();
	return 0;
}



It doesn't seem to be giving me a list of the questions that are incorrectly answered, or the missed number of questions.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,667
  • Joined: 25-December 09

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 10:11 PM

Quote

It doesn't seem to be giving me a list of the questions that are incorrectly answered

The if statement below will always evaluate to true because you are using the assignment operator "=" when you should be using the comparison operator "==".
if(CorrectAnswers[count] = StudentAnswers[count])



Jim
Was This Post Helpful? 0
  • +
  • -

#14 sharkman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-July 11

Re: Comparing 2 Arrays, Run-Time Check Failure#2

Posted 17 July 2011 - 10:13 PM

I knew it was something small like that! Thank you!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1