C++ Array comparison not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C++ Array comparison not working

  1. #1
    Join Date
    Nov 2012
    Posts
    4

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

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  3. #3
    Join Date
    Nov 2012
    Posts
    4

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    Re: C++ Array comparison not working

    Quote Originally Posted by slappy5star View Post
    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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center