Comparison of arrays created from a class
Nov 3, 2012 at 8:29pm UTC
I need to create a program that creates an array of a class object and then compares it to another array (both user input). I am having a problem calling and correctly comparing these arrays and could use some pointers. Here is my code:
Also, any pointers as far as how the rest of the program is built would be appreciated, but I am simply trying to understand how to correctly compare these arrays!
Thanks so much!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
const int NUM_QUESTIONS = 20;
class TestGrader
{
private :
char correctAnswers[NUM_QUESTIONS];
char studentAnswers[NUM_QUESTIONS];
public :
void setKey ();
void grade (char a[], char b[]);
};
int main ()
{
char answers = ' ' ,
choices = ' ' ;
cout << "This program will score your drivers exam.\n" ;
cout << "Please enter all answers in capital letters.\n" ;
cout << endl;
//creates 2 array objects of size 20
TestGrader correctAnswers[NUM_QUESTIONS];
TestGrader studentAnswers[NUM_QUESTIONS];
correctAnswers[answers].setKey();
studentAnswers[choices].grade(); //This is where I think I need help!
return 0;
}
/*********TestGrader::setKey************/
//This function gets the correct answers
//and stores them in an array
void TestGrader::setKey ()
{
for (int count = 0; count < NUM_QUESTIONS; count++)
{
cout << "What is the answer to question #" << (count+1) << ":" ;
cin >> correctAnswers[count];
while (correctAnswers[count] != 'A' && correctAnswers[count] != 'B' && correctAnswers[count] != 'C' && correctAnswers[count] != 'D' )
{
cout << "You can only enter A, B, C, or D.\n" ;
cout << "What is the answer to question #" << count << ":" ;
cin >> correctAnswers[count];
}
}
}
/**********TestGrader::grade*************/
//This function gets the test takers
//answer and compares them to the key
void TestGrader::grade (char a[], char b[])
{
int correct = 0;
for (int count = 0; count < NUM_QUESTIONS; count++)
{
cout << "Enter the test takers answer to question #" << (count+1) << ":" ;
cin >> studentAnswers[count];
while (studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && studentAnswers[count] != 'C' && studentAnswers[count] != 'D' )
{
cout << "You can only enter A, B, C, or D.\n" ;
cout << "Enter the test takers answer to question #" << (count+1) << ":" ;
cin >> studentAnswers[count];
}
}
for (int count = 0; count < NUM_QUESTIONS; count++)
{
if (correctAnswers[count] == studentAnswers[count])
correct++;
}
if (correct < 15)
{
cout << "You failed the exam. You need 15 correct questions to pass.\n" ;
cout << "You only got " << correct << "questions correct." ;
}
else
{
cout << "You passed! You got " << correct << "questions right!\n" ;
}
}
Nov 3, 2012 at 9:03pm UTC
Your TestGrader has two arrays of size 20. You make two sets of TestGraders in main, both being size 20. You have 400 ints in your program.
This project is a little strange, but such is life. If I'm to make an array of classes, I would have the class be:
1 2 3 4 5 6 7 8 9 10 11 12 13
class TestGrader
{
private char score;
void set(void ) { cin >> score; /* and what not */ }
int get(void ) { return score; }
// Without an operator overload...
bool correct(TestGrader& other)
{
return score == other.get();
}
};
And in main:
TestGrader real[20], possible[20];
Last edited on Nov 3, 2012 at 9:04pm UTC
Topic archived. No new replies allowed.