-
November 3rd, 2012, 11:06 AM
#1
C++ Array comparison not working
The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers. Any idea whats causing this problem? Thanks!
#include <iostream>
using namespace std;
const int QUESTION = 20;
//class for testing grades
class TestGrader
{
private:
char answerKey[QUESTION];
char testAnswers[QUESTION];
char answer;
public:
void setKey (char answer);
void grade (char a);
};
int main ()
{
char answer = ' ';
char testAnswers = ' ';
//creates a grade object
TestGrader answers [QUESTION];
TestGrader testAnswer [QUESTION];
//calls setKey to get correct answers
answers[answer].setKey(answer);
testAnswer[testAnswers].grade(testAnswers);
return 0;
}
/***********setKey*************/
//This class function sets an
//answer key for the driving test
void TestGrader::setKey (char answer)
{
for (int count = 1; count <= QUESTION; count++)
{
cout << "What is the correct answer for question #" << (count) << ":";
cin >> answerKey[answer];
while (answerKey[answer] != 'A' && answerKey[answer] != 'B' && answerKey[answer] != 'C' && answerKey[answer] != 'D')
{
cout << "You must enter A, B, C, or D\n";
cout << "What is the correct answer for question #" << (count) << ":";
cin >> answerKey[answer];
}
}
}
/***********grade**************/
//This class function takes the
//answers as input to an array
//and compares it to the key
void TestGrader::grade (char a)
{
int correctAnswer = 0;
for (int count = 1; count <= QUESTION; count++)
{
cout << "What is the test takers answer to question #" << count << ":";
cin >> testAnswers[a];
while (testAnswers[a] != 'A' && testAnswers[a] != 'B' && testAnswers[a] != 'C' && testAnswers[a] != 'D')
{
cout << "You must enter A, B, C, or D\n";
cout << "What is the test takers answer to question #" << count << ":";
cin >> testAnswers[a];
}
}
for (int correct = 0; correct < QUESTION; correct++)
{
if (answerKey[answer] == testAnswers[a])
correctAnswer++;
}
if (correctAnswer < 15)
cout << "You failed, you must get at least 15 correct answers.\n You only got " << correctAnswer << " questions right.\n";
else
cout << "You passed! You got " << correctAnswer << " questions correct.\n";
}
-
November 3rd, 2012, 11:47 AM
#2
Re: C++ Array comparison not working
Code:
for (int correct = 0; correct < QUESTION; correct++)
{
if (answerKey[answer] == testAnswers[a])
correctAnswer++;
}
You keep comparing the same thing. 'a' and 'answer' never change inside the loop.
Also, please use code tags for posting code.
-
November 3rd, 2012, 12:19 PM
#3
Re: C++ Array comparison not working
Sorry, I did not realize the site supported the tags, thanks for the heads up!
So, what elements should I be comparing there? I assumed answer was being taken from the previous input and 'a' was being taken from the current input.
I made a change that seems to be helping (I am moving in the right direction..I hope).
Code:
for (int correct = 0; correct < QUESTION; correct++)
{
if (answerKey[correct] == testAnswers[correct])
correctAnswer++;
}
But now it is always all correct...
Last edited by slappy5star; November 3rd, 2012 at 12:30 PM.
-
November 3rd, 2012, 01:06 PM
#4
Re: C++ Array comparison not working
Originally Posted by slappy5star
Sorry, I did not realize the site supported the tags, thanks for the heads up!
...
I made a change that seems to be helping (I am moving in the right direction..I hope).
Code:
for (int correct = 0; correct < QUESTION; correct++)
{
if (answerKey[correct] == testAnswers[correct])
correctAnswer++;
}
Note that Code tags themselves do NOT insert proper code indentations! You must paste already formatted code between the code tags!
Victor Nijegorodov
-
November 3rd, 2012, 01:27 PM
#5
Re: C++ Array comparison not working
You've got a pretty fundamental disconnect in your use of arrays.
I had started to try to explain the errors, but your code is so messed up, there isn't much point. Go back and read the chapter on arrays again. When you're done, read it again. Work through the examples in your text if there are any. It looks like you're trying to guess how it all works, and that's never a good plan.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|