fork download
  1. //Devin Scheu CS1A Chapter 7, P. 446, #9
  2. //
  3. /**************************************************************
  4. *
  5. * GRADE DRIVER'S LICENSE EXAM
  6. * ____________________________________________________________
  7. * This program determines if a student passed the driver's
  8. * license exam, the number of correct and incorrect answers,
  9. * and the questions answered incorrectly.
  10. * ____________________________________________________________
  11. * INPUT
  12. * studentAnswers : The student's answers for the 20 questions
  13. *
  14. * OUTPUT
  15. * passStatus : Whether the student passed or failed
  16. * correctCount : Number of correctly answered questions
  17. * incorrectCount : Number of incorrectly answered questions
  18. * incorrectQuestions : List of incorrectly answered question numbers
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <string>
  25.  
  26. using namespace std;
  27.  
  28. int main () {
  29.  
  30. //Variable Declarations
  31. const int NUM_QUESTIONS = 20; //OUTPUT - Number of questions on the exam
  32. char correctAnswers[NUM_QUESTIONS] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
  33. 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
  34. char studentAnswers[NUM_QUESTIONS]; //INPUT - The student's answers for the 20 questions
  35. int correctCount = 0; //OUTPUT - Number of correctly answered questions
  36. int incorrectCount; //OUTPUT - Number of incorrectly answered questions
  37. string passStatus; //OUTPUT - Whether the student passed or failed
  38. string incorrectQuestions; //OUTPUT - List of incorrectly answered question numbers
  39.  
  40. //Prompt for Input
  41. for (int i = 0; i < NUM_QUESTIONS; i++) {
  42. cout << "Enter answer for question " << (i + 1) << " (A/B/C/D): ";
  43. cin >> studentAnswers[i];
  44. studentAnswers[i] = toupper(studentAnswers[i]);
  45. while (studentAnswers[i] != 'A' && studentAnswers[i] != 'B' && studentAnswers[i] != 'C' && studentAnswers[i] != 'D') {
  46. cout << "\nError: Please enter A, B, C, or D: ";
  47. cin >> studentAnswers[i];
  48. studentAnswers[i] = toupper(studentAnswers[i]);
  49. }
  50. cout << studentAnswers[i] << endl;
  51. }
  52.  
  53. //Grade the Exam
  54. for (int i = 0; i < NUM_QUESTIONS; i++) {
  55. if (studentAnswers[i] == correctAnswers[i]) {
  56. correctCount++;
  57. }
  58. }
  59. incorrectCount = NUM_QUESTIONS - correctCount;
  60. passStatus = (correctCount >= 15) ? "Passed" : "Failed";
  61.  
  62. //Collect Incorrect Questions
  63. bool first = true;
  64. for (int i = 0; i < NUM_QUESTIONS; i++) {
  65. if (studentAnswers[i] != correctAnswers[i]) {
  66. if (!first) {
  67. incorrectQuestions += ", ";
  68. }
  69. incorrectQuestions += to_string(i + 1);
  70. first = false;
  71. }
  72. }
  73. if (incorrectQuestions.empty()) {
  74. incorrectQuestions = "None";
  75. }
  76.  
  77. //Separator and Output Section
  78. cout << "-------------------------------------------------------" << endl;
  79. cout << "OUTPUT:" << endl;
  80.  
  81. //Output Result
  82. cout << left << setw(25) << "Exam Status:" << right << setw(15) << passStatus << endl;
  83. cout << left << setw(25) << "Correct Answers:" << right << setw(15) << correctCount << endl;
  84. cout << left << setw(25) << "Incorrect Answers:" << right << setw(15) << incorrectCount << endl;
  85. cout << left << setw(25) << "Incorrect Questions:" << right << setw(15) << incorrectQuestions << endl;
  86.  
  87. } //end of main()
Success #stdin #stdout 0.01s 5320KB
stdin
B
D
A
A
C
C
B
A
C
D
B
C
D
C
D
C
D
B
D
A
stdout
Enter answer for question 1 (A/B/C/D): B
Enter answer for question 2 (A/B/C/D): D
Enter answer for question 3 (A/B/C/D): A
Enter answer for question 4 (A/B/C/D): A
Enter answer for question 5 (A/B/C/D): C
Enter answer for question 6 (A/B/C/D): C
Enter answer for question 7 (A/B/C/D): B
Enter answer for question 8 (A/B/C/D): A
Enter answer for question 9 (A/B/C/D): C
Enter answer for question 10 (A/B/C/D): D
Enter answer for question 11 (A/B/C/D): B
Enter answer for question 12 (A/B/C/D): C
Enter answer for question 13 (A/B/C/D): D
Enter answer for question 14 (A/B/C/D): C
Enter answer for question 15 (A/B/C/D): D
Enter answer for question 16 (A/B/C/D): C
Enter answer for question 17 (A/B/C/D): D
Enter answer for question 18 (A/B/C/D): B
Enter answer for question 19 (A/B/C/D): D
Enter answer for question 20 (A/B/C/D): A
-------------------------------------------------------
OUTPUT:
Exam Status:                      Passed
Correct Answers:                      17
Incorrect Answers:                     3
Incorrect Questions:           6, 14, 17