|
-
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";
}
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
|