fork download
  1. // Timothy Price CS1A Ch 7 challenge 9
  2. /*******************************************************************************
  3.  * Determine grade
  4.  *
  5.  * This program receives character inputs and determines if a student has passed
  6.  * or failed.
  7.  *
  8.  * Inputs:
  9.  * Question answers
  10.  *
  11.  * Outputs:
  12.  * Pass or fail indication
  13.  * Score
  14.  * Incorrect answers
  15.  ******************************************************************************/
  16.  
  17. #include <iostream>
  18. #include <cctype> // Required for toupper()
  19.  
  20. using namespace std;
  21.  
  22. int main() {
  23. const int NUM_QUESTIONS = 20;
  24.  
  25. // Array storing the correct answers
  26. char correctAnswers[NUM_QUESTIONS] = {
  27. 'B', 'D', 'A', 'A', 'C',
  28. 'A', 'B', 'A', 'C', 'D',
  29. 'B', 'C', 'D', 'A', 'D',
  30. 'C', 'C', 'B', 'D', 'A'
  31. };
  32.  
  33. // Array to store the student's answers
  34. char studentAnswers[NUM_QUESTIONS];
  35.  
  36. cout << "-------------------------------------" << endl;
  37. cout << " DRIVER'S LICENSE EXAM GRADER " << endl;
  38. cout << "-------------------------------------" << endl << endl;
  39.  
  40. // Get student answers with input validation
  41. for (int i = 0; i < NUM_QUESTIONS; ++i) {
  42. char ans;
  43. do {
  44. cout << "Question " << (i + 1) << ": ";
  45. cin >> ans;
  46. ans = toupper(ans); // Convert to uppercase for convenience
  47. cout << ans << endl;
  48.  
  49. // Input validation
  50. if (ans != 'A' && ans != 'B' && ans != 'C' && ans != 'D') {
  51. cout << "Invalid selection. Try again: " << endl;
  52. cin >> ans;
  53. }
  54. } while (ans != 'A' && ans != 'B' && ans != 'C' && ans != 'D');
  55. studentAnswers[i] = ans;
  56. }
  57.  
  58. // Evaluation variables
  59. int correctCount = 0;
  60. int incorrectCount = 0;
  61. int incorrectQuestions[NUM_QUESTIONS]; // numbers of wrong answers
  62.  
  63. for (int i = 0; i < NUM_QUESTIONS; ++i) {
  64. if (studentAnswers[i] == correctAnswers[i]) {
  65. correctCount++;
  66. } else {
  67. incorrectQuestions[incorrectCount] = i + 1;
  68. incorrectCount++;
  69. }
  70. }
  71.  
  72. // Display Results
  73. cout << endl << "-------------------------------------" << endl;
  74. cout << " EXAM RESULTS " << endl;
  75. cout << "-------------------------------------" << endl;
  76.  
  77. // Pass/Fail check (Passing score is 15 or more out of 20)
  78. if (correctCount >= 15) {
  79. cout << "Status: PASSED" << endl;
  80. } else {
  81. cout << "Status: FAILED" << endl;
  82. }
  83.  
  84. cout << "Total Answered Correctly: " << correctCount << " / ";
  85. cout << NUM_QUESTIONS << endl << "Total Answered Incorrectly: ";
  86. cout << incorrectCount << " / " << NUM_QUESTIONS << endl;
  87.  
  88. // Display list of incorrect questions, if any
  89. if (incorrectCount > 0) {
  90. cout << endl << "Questions answered incorrectly:" << endl;
  91. for (int i = 0; i < incorrectCount; ++i) {
  92. cout << "Question " << incorrectQuestions[i] << " (You answered: ";
  93. cout << studentAnswers[incorrectQuestions[i] - 1];
  94. cout << ", Correct: " << correctAnswers[incorrectQuestions[i] - 1];
  95. cout << ")" << endl;
  96. }
  97. } else {
  98. cout << endl << "Congratulations! Perfect score!" << endl;
  99. }
  100.  
  101. cout << "-------------------------------------" << endl;
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0.01s 5316KB
stdin
b
d
a
a
c
a
b
a
c
d
b
c
d
a
d
c
c
a
c
d
stdout
-------------------------------------
   DRIVER'S LICENSE EXAM GRADER      
-------------------------------------

Question 1: B
Question 2: D
Question 3: A
Question 4: A
Question 5: C
Question 6: A
Question 7: B
Question 8: A
Question 9: C
Question 10: D
Question 11: B
Question 12: C
Question 13: D
Question 14: A
Question 15: D
Question 16: C
Question 17: C
Question 18: A
Question 19: C
Question 20: D

-------------------------------------
           EXAM RESULTS              
-------------------------------------
Status: PASSED
Total Answered Correctly:   17 / 20
Total Answered Incorrectly: 3 / 20

Questions answered incorrectly:
Question 18 (You answered: A, Correct: B)
Question 19 (You answered: C, Correct: D)
Question 20 (You answered: D, Correct: A)
-------------------------------------