fork download
  1. // Saliha Babar CS1A Chapter 7, Page 446, #9
  2. //
  3. /***************************************************************************
  4.  * GRADE MULTIPLE CHOICE QUESTIONS
  5.  * _________________________________________________________________________
  6.  * This program accepts answers for 20 multiple choice questions, in form of
  7.  * A, B, C, or D. Then it calculates total correct answers, total incorrect
  8.  * answers as well as incorrect questions list.
  9.  *
  10.  * There is no specific formula for this program
  11.  * ___________________________________________________________________________
  12.  * INPUT
  13.  * TOTAL_QUESTIONS : total question in an exam
  14.  * userAns : user answer for questions
  15.  * schemeAns : correct answer for questions (marking scheme)
  16.  * OUTPUT
  17.  * totalCorrect : total correct answer entered by user
  18.  * *************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22.  
  23. char getUserAns( int QuestionCount );
  24. void compareAnswers ( const char user [], const char scheme[] , int TOTAL_QUESTIONS);
  25.  
  26. int main() {
  27. int const TOTAL_QUESTIONS = 20; // INPUT - total question in exam
  28. char userAns[TOTAL_QUESTIONS]; // INPUT - user input
  29. char schemeAns[TOTAL_QUESTIONS] = {'B', 'D', 'A', 'A', 'C',
  30. 'A', 'B', 'A', 'C', 'D',
  31. 'B', 'C', 'D', 'A', 'D',
  32. 'C', 'C', 'B', 'D', 'A'};
  33.  
  34. // Get the user answer for 20 questions
  35. for (int i = 0 ; i < 20 ; i++)
  36. {
  37. userAns[i] = getUserAns(i);
  38. }
  39.  
  40. // Call the void function to make decision
  41. compareAnswers ( userAns, schemeAns, TOTAL_QUESTIONS );
  42.  
  43. return 0;
  44. }
  45.  
  46. char getUserAns( int QuestionCount )
  47. {
  48. char input;
  49. cout << "Enter the answer for Question " << (QuestionCount + 1);
  50. cin >> input;
  51.  
  52. while ( input != 'A' && input != 'B' && input != 'C' && input != 'D')
  53. {
  54. cout << "Only enter letters A,B,C or D. Enter your answer again";
  55. cin >> input;
  56. }
  57. cout << endl;
  58.  
  59. return input;
  60. }
  61.  
  62.  
  63. void compareAnswers ( const char user [], const char scheme[] , int TOTAL_QUESTIONS)
  64. {
  65. int totalCorrect = 0; // OUTPUT - total correct answer entered
  66.  
  67. for ( int i = 0; i < TOTAL_QUESTIONS ; i++)
  68. {
  69. if ( user[i] == scheme[i])
  70. {
  71. totalCorrect += 1;
  72. }
  73.  
  74. else
  75. {
  76. cout << "Incorrect Answer for question #" << (i+1) << endl;
  77. }
  78. }
  79.  
  80. cout << "Total correct answer(s) is " << totalCorrect << "/20\n";
  81. cout << "Total incorrect answer(s) is " << (TOTAL_QUESTIONS - totalCorrect) << "/20\n";
  82.  
  83. if ( totalCorrect >= 15)
  84. {
  85. cout << "Congrats, you passed the test.\n";
  86. }
  87. else
  88. {
  89. cout << "Sorry, you failed the test.\n";
  90. }
  91.  
  92. }
Success #stdin #stdout 0s 5284KB
stdin
BDAAC
ABACD
BCDAD
CCBDA
stdout
Enter the answer for Question 1
Enter the answer for Question 2
Enter the answer for Question 3
Enter the answer for Question 4
Enter the answer for Question 5
Enter the answer for Question 6
Enter the answer for Question 7
Enter the answer for Question 8
Enter the answer for Question 9
Enter the answer for Question 10
Enter the answer for Question 11
Enter the answer for Question 12
Enter the answer for Question 13
Enter the answer for Question 14
Enter the answer for Question 15
Enter the answer for Question 16
Enter the answer for Question 17
Enter the answer for Question 18
Enter the answer for Question 19
Enter the answer for Question 20
Total correct answer(s) is 20/20
Total incorrect answer(s) is 0/20
Congrats, you passed the test.