//Devin Scheu CS1A Chapter 7, P. 446, #9
//
/**************************************************************
*
* GRADE DRIVER'S LICENSE EXAM
* ____________________________________________________________
* This program determines if a student passed the driver's
* license exam, the number of correct and incorrect answers,
* and the questions answered incorrectly.
* ____________________________________________________________
* INPUT
* studentAnswers : The student's answers for the 20 questions
*
* OUTPUT
* passStatus : Whether the student passed or failed
* correctCount : Number of correctly answered questions
* incorrectCount : Number of incorrectly answered questions
* incorrectQuestions : List of incorrectly answered question numbers
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
//Variable Declarations
const int NUM_QUESTIONS = 20; //OUTPUT - Number of questions on the exam
char correctAnswers[NUM_QUESTIONS] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
char studentAnswers[NUM_QUESTIONS]; //INPUT - The student's answers for the 20 questions
int correctCount = 0; //OUTPUT - Number of correctly answered questions
int incorrectCount; //OUTPUT - Number of incorrectly answered questions
string passStatus; //OUTPUT - Whether the student passed or failed
string incorrectQuestions; //OUTPUT - List of incorrectly answered question numbers
//Prompt for Input
for (int i = 0; i < NUM_QUESTIONS; i++) {
cout << "Enter answer for question " << (i + 1) << " (A/B/C/D): ";
cin >> studentAnswers[i];
studentAnswers[i] = toupper(studentAnswers[i]);
while (studentAnswers[i] != 'A' && studentAnswers[i] != 'B' && studentAnswers[i] != 'C' && studentAnswers[i] != 'D') {
cout << "\nError: Please enter A, B, C, or D: ";
cin >> studentAnswers[i];
studentAnswers[i] = toupper(studentAnswers[i]);
}
cout << studentAnswers[i] << endl;
}
//Grade the Exam
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (studentAnswers[i] == correctAnswers[i]) {
correctCount++;
}
}
incorrectCount = NUM_QUESTIONS - correctCount;
passStatus = (correctCount >= 15) ? "Passed" : "Failed";
//Collect Incorrect Questions
bool first = true;
for (int i = 0; i < NUM_QUESTIONS; i++) {
if (studentAnswers[i] != correctAnswers[i]) {
if (!first) {
incorrectQuestions += ", ";
}
incorrectQuestions += to_string(i + 1);
first = false;
}
}
if (incorrectQuestions.empty()) {
incorrectQuestions = "None";
}
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Output Result
cout << left << setw(25) << "Exam Status:" << right << setw(15) << passStatus << endl;
cout << left << setw(25) << "Correct Answers:" << right << setw(15) << correctCount << endl;
cout << left << setw(25) << "Incorrect Answers:" << right << setw(15) << incorrectCount << endl;
cout << left << setw(25) << "Incorrect Questions:" << right << setw(15) << incorrectQuestions << endl;
} //end of main()