Comparing Arrays question

This is a discussion on Comparing Arrays question within the C++ Programming forums, part of the General Programming Boards category; For any input i type in I keep getting the same thing. It keeps saying that all of the answers ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Comparing Arrays question

    For any input i type in I keep getting the same thing. It keeps saying that all of the answers are incorrect. I'm also getting these lines of output every time.
    You got 20 correct.
    You missed -20.
    You've passed!

    Can someone please help me?
    Here's the description of the problem. My code is below it.

    The local Driver's License Office has asked you to write aprogram that grades the written portion of driver's license exam.The exam has 20 multiple choice questions. Here are the correctanswers:
    1. B 6.A 11.B 16. C
    2. D 7.B 12.C 17. C
    3. A 8.A 13.D 18. B
    4. A 9.C 14.A 19. D
    5. C 10.D 15.D 20. A
    Your program should store the correct answers shown above inan array. It should ask the user to enter the student's answers foreach of the 20 questions, and the answers should be stored inanother array. After the student's answers have been entered, theprogram should display a message indicating whether the studentpassed or failed the exam. (A student must correctly answer 15 ofthe 20 questions to pass the exam.)
    It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the questions numbers of the incorrectly answered questions.
    Input Validations: Only accept the letters A, B, C, or D as answers.


    Code:
    
    
    
    
    //Write the program described on page 446 -- question 9.  The question is titled "Driver's License Exam".
    #include <iostream>
    using namespace std;
    
    
    //function prototypes
    void get_answers(char[], int);
    int calculate_grade(char[], char[],int, int);
    void output_grade(int, int);
    
    
    int main()
    {
        //constent variable to store the number of answers/questions
        const int number_of_answers = 20;
        int answered_correctly = 0;
        //variable to store right answers
        char right_answers[number_of_answers] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
    
    
                                                'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
    
    
        char enter_answers[number_of_answers];
    
    
        cout << "Driver's License Exam Grader" << endl;
    
    
        //function to get answers from the user
        get_answers(enter_answers, number_of_answers);
        //function to calculate grade
        calculate_grade(right_answers, enter_answers,number_of_answers, answered_correctly);
        //function to output if the user passed or failed
        output_grade(number_of_answers,answered_correctly);
    
    
        return 0;
    }
    
    
    void get_answers(char enter_answers[], int number_of_answers)
    {
        //ask the user to enter the 20 answers
        for (int counter=0; counter < number_of_answers; counter++)
        {
            cout << "Enter your answer for question " << (counter + 1) << endl;
            cin >> enter_answers[counter];
    
    
             while (enter_answers[counter] != 'A' && enter_answers[counter] != 'B' &&
                    enter_answers[counter] != 'C' && enter_answers[counter] != 'D' )
            {
                cout << "ERROR: Only A,B,C, and D are valid.\n";
                cout << "Question " << (counter + 1) << ": ";
                cin >> enter_answers[counter];
            }
        }
    
    
    }
    
    
    
    
        int calculate_grade(char right_answers[], char enter_answers[],int number_of_answers, int answered_correctly)
        {
             for(int x=0; x <= number_of_answers; x++)
             {
                 if (right_answers[x] == enter_answers[x])
                 {
                    answered_correctly ++;
                 }
    
    
    
    
                return answered_correctly;
    
    
             }
        }
    
    
    void output_grade(char right_answers, char enter_answers, int answered_correctly, int number_of_answers)
    {
    
    
        int missed = number_of_answers - answered_correctly;
        for (int x=0; x< number_of_answers; x++)
             {
                if (right_answers[x]!= enter_answers[x])
                {
                    for (x = 0; x<number_of_answers; x++)
                    {
                        cout << "Answer " << (x+1)<< " is incorrect" << endl;
                    }
                }
             }
        }
    
    
    
    
    
    
    
    
        cout << "Results (minimum 15 correct to pass): " << endl;
        cout << "You got " << answered_correctly << " correct." << endl;
        cout << "You missed " << missed << endl;
        if (answered_correctly >=15)
        {
            cout<< "You've passed!" << endl;
        }
        else
        {
            cout << "You'ved failed." << endl;
        }
    }
    Last edited by Guy_from_SoCal; 11-09-2012 at 08:38 PM. Reason: I had forgotten to return the value of calculate_grade function, but now it doesn't run. What am i doing wrong?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Code:
     void calculate_grade(char right_answers[], char enter_answers[],int number_of_answers, int answered_correctly)
        {
             for(int x=0; x <= number_of_answers; x++)
             {
                 if (right_answers[x] == enter_answers[x])
                 answered_correctly +=1;
             }
          ...
    Ok you compute the number of correct answers.What will happen when the program ends?You do not pass the variable by reference ,either return it, so the change of value will be lost when the function terminates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing two arrays
    By Gonz in forum C Programming
    Replies: 2
    Last Post: 03-23-2011, 12:38 AM
  2. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  3. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  4. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 04:52 PM
  5. Replies: 7
    Last Post: 04-13-2003, 10:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21