fork download
  1. // Marvyn Jesse De La Torre CS1A Chapter 7, pg. 446, #9
  2. //
  3. /****************************************************************************
  4.  * GRADE THE DRIVER'S LICENSE EXAM
  5.  *
  6.  * This program accepts and compares a student's answers to the correct answers for a
  7.  * 20-question driver's license exam. It displays whether the student passed
  8.  * or failed, the number of correct and incorrect answers, and the question
  9.  * numbers answered incorrectly.
  10.  *
  11.  * INPUT
  12.  * studentAnswers // The student's answers to the 20 questions
  13.  *
  14.  * OUTPUT
  15.  * correctCount // Number of correct answers
  16.  * incorrectCount // Number of incorrect answers
  17.  * missedQuestions // Question numbers answered incorrectly
  18.  * examResult // Whether the student passed or failed
  19.  ****************************************************************************/
  20.  
  21. #include <iostream>
  22. #include <cctype>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. const int QUESTIONS = 20;
  28. const int PASSING_SCORE = 15;
  29.  
  30. // Store the correct answers for the exam.
  31. const char correctAnswers[QUESTIONS] =
  32. {
  33. 'B', 'D', 'A', 'A', 'C',
  34. 'A', 'B', 'A', 'C', 'D',
  35. 'B', 'C', 'D', 'A', 'D',
  36. 'C', 'C', 'B', 'D', 'A'
  37. };
  38.  
  39. char studentAnswers[QUESTIONS];
  40. int correctCount = 0;
  41. int incorrectCount = 0;
  42.  
  43. // Get and validate the student's answer to each question.
  44. for (int i = 0; i < QUESTIONS; i++)
  45. {
  46. cout << "Enter the answer for question " << i + 1
  47. << " (A, B, C, or D): ";
  48. cin >> studentAnswers[i];
  49. studentAnswers[i] = toupper(studentAnswers[i]);
  50.  
  51. while (cin.fail() ||
  52. (studentAnswers[i] != 'A' &&
  53. studentAnswers[i] != 'B' &&
  54. studentAnswers[i] != 'C' &&
  55. studentAnswers[i] != 'D'))
  56. {
  57. cout << "Invalid answer. Enter A, B, C, or D: ";
  58. cin.clear();
  59. cin >> studentAnswers[i];
  60. studentAnswers[i] = toupper(studentAnswers[i]);
  61. }
  62.  
  63. // Compare the student's answer with the correct answer.
  64. if (studentAnswers[i] == correctAnswers[i])
  65. {
  66. correctCount++;
  67. }
  68. else
  69. {
  70. incorrectCount++;
  71. }
  72. }
  73.  
  74. // Display the exam result and number of correct and incorrect answers.
  75. cout << "\nDriver's License Exam Results\n";
  76. cout << "------------------------------\n";
  77. cout << "Correct answers: " << correctCount << endl;
  78. cout << "Incorrect answers: " << incorrectCount << endl;
  79.  
  80. if (correctCount >= PASSING_SCORE)
  81. {
  82. cout << "Result: Passed\n";
  83. }
  84. else
  85. {
  86. cout << "Result: Failed\n";
  87. }
  88.  
  89. // Display the question numbers that were answered incorrectly.
  90. cout << "\nQuestions answered incorrectly: ";
  91. if (incorrectCount == 0)
  92. {
  93. cout << "None";
  94. }
  95. else
  96. {
  97. for (int i = 0; i < QUESTIONS; i++)
  98. {
  99. if (studentAnswers[i] != correctAnswers[i])
  100. {
  101. cout << i + 1 << " ";
  102. }
  103. }
  104. }
  105. cout << endl;
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0s 5320KB
stdin
B
D
A
A
C
A
B
A
C
D
B
C
D
A
D
C
C
B
D
A
stdout
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