// 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; }
B D A A C A B A C D B C D A D C C B D A
Enter the answer for question 1 (A, B, C, or D): Enter the answer for question 2 (A, B, C, or D): Enter the answer for question 3 (A, B, C, or D): Enter the answer for question 4 (A, B, C, or D): Enter the answer for question 5 (A, B, C, or D): Enter the answer for question 6 (A, B, C, or D): Enter the answer for question 7 (A, B, C, or D): Enter the answer for question 8 (A, B, C, or D): Enter the answer for question 9 (A, B, C, or D): Enter the answer for question 10 (A, B, C, or D): Enter the answer for question 11 (A, B, C, or D): Enter the answer for question 12 (A, B, C, or D): Enter the answer for question 13 (A, B, C, or D): Enter the answer for question 14 (A, B, C, or D): Enter the answer for question 15 (A, B, C, or D): Enter the answer for question 16 (A, B, C, or D): Enter the answer for question 17 (A, B, C, or D): Enter the answer for question 18 (A, B, C, or D): Enter the answer for question 19 (A, B, C, or D): Enter the answer for question 20 (A, B, C, or D): Driver's License Exam Results ------------------------------ Correct answers: 20 Incorrect answers: 0 Result: Passed Questions answered incorrectly: None