// Marvyn Jesse De La Torre       CS1A       Chapter 7, pg. 446, #9
//
/****************************************************************************
 * GRADE THE DRIVER'S LICENSE EXAM
 *
 * This program accepts and compares a student's answers to the correct answers for a
 * 20-question driver's license exam. It displays whether the student passed
 * or failed, the number of correct and incorrect answers, and the question
 * numbers answered incorrectly.
 *
 * INPUT
 *   studentAnswers   // The student's answers to the 20 questions
 *
 * OUTPUT
 *   correctCount     // Number of correct answers
 *   incorrectCount   // Number of incorrect answers
 *   missedQuestions  // Question numbers answered incorrectly
 *   examResult       // Whether the student passed or failed
 ****************************************************************************/

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    const int QUESTIONS = 20;
    const int PASSING_SCORE = 15;

    // Store the correct answers for the exam.
    const char correctAnswers[QUESTIONS] =
    {
        'B', 'D', 'A', 'A', 'C',
        'A', 'B', 'A', 'C', 'D',
        'B', 'C', 'D', 'A', 'D',
        'C', 'C', 'B', 'D', 'A'
    };

    char studentAnswers[QUESTIONS];
    int correctCount = 0;
    int incorrectCount = 0;

    // Get and validate the student's answer to each question.
    for (int i = 0; i < QUESTIONS; i++)
    {
        cout << "Enter the answer for question " << i + 1
             << " (A, B, C, or D): ";
        cin >> studentAnswers[i];
        studentAnswers[i] = toupper(studentAnswers[i]);

        while (cin.fail() ||
               (studentAnswers[i] != 'A' &&
                studentAnswers[i] != 'B' &&
                studentAnswers[i] != 'C' &&
                studentAnswers[i] != 'D'))
        {
            cout << "Invalid answer. Enter A, B, C, or D: ";
            cin.clear();
            cin >> studentAnswers[i];
            studentAnswers[i] = toupper(studentAnswers[i]);
        }

        // Compare the student's answer with the correct answer.
        if (studentAnswers[i] == correctAnswers[i])
        {
            correctCount++;
        }
        else
        {
            incorrectCount++;
        }
    }

    // Display the exam result and number of correct and incorrect answers.
    cout << "\nDriver's License Exam Results\n";
    cout << "------------------------------\n";
    cout << "Correct answers: " << correctCount << endl;
    cout << "Incorrect answers: " << incorrectCount << endl;

    if (correctCount >= PASSING_SCORE)
    {
        cout << "Result: Passed\n";
    }
    else
    {
        cout << "Result: Failed\n";
    }

    // Display the question numbers that were answered incorrectly.
    cout << "\nQuestions answered incorrectly: ";
    if (incorrectCount == 0)
    {
        cout << "None";
    }
    else
    {
        for (int i = 0; i < QUESTIONS; i++)
        {
            if (studentAnswers[i] != correctAnswers[i])
            {
                cout << i + 1 << " ";
            }
        }
    }
    cout << endl;

    return 0;
}