I am having problems comparing the arrays in the following code. I commented where I "think" the problem is. Any help would be appreciated.

#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;

    TestGrader correctAnswers;
    TestGrader studentAnswers;

    correctAnswers.setKey();
    studentAnswers.grade(); //what do I do here? Or is the problem someplace else?

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

}
//in line 31 and 32 should be something like:
    correctAnswers[answers].setKey();
    studentAnswers[choices].grade();

Corrections: There are three corrections , in Line Nos 32,49,76
and they are

studentAnswers.grade(&answers,&choices);
cout << "What is the answer to question #" << (count+1) << ":";
if (!(correctAnswers[count] == studentAnswers[count]))

made this corrections in your program, definitely it will run successfully !!!

This article has been dead for over six months. Start a new discussion instead.