1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
void checkAnswers(char[], char[], int, int); // Prototype for the check answers function.
void displayResult(int correctAnswers);
int main()
{
const int numQuestions = 20; // Setting up labeled variables.
const int minCorrect = 15;
char examAnswers[numQuestions] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'D', 'D', 'A'};
char inputAnswers[numQuestions]; // User input Array, above is the correct answer array.
cout << "Welcome to the Florida State Driver's License Exam!\n";
cout << "There is a total of 20 questions on this exam. You must answer at least 15 questions correctly to pass.\n";
cout << "Good luck!\n\n";
for (int response = 0; response < numQuestions; response++) // Loop for the user's input.
{
cout << "Question #" << (response + 1) << ": ";
cin >> inputAnswers[response];
inputAnswers[response] = toupper(inputAnswers[response]);
while (inputAnswers[response] != 'A' && inputAnswers[response] != 'B' && inputAnswers[response] != 'C' && inputAnswers[response] != 'D')
{
cout << "You must enter A, B, C, or D." << endl; // The while loop is used for user input validation.
cout << "Question #" << (response + 1) << ": ";
cin >> inputAnswers[response];
inputAnswers[response] = toupper(inputAnswers[response]);
}
}
cout << "\n";
checkAnswers(examAnswers, inputAnswers, numQuestions, minCorrect); // Checks the users answer as a function.
displayResult(correctAnswers);
cin.ignore(1);
return 0;
}
void checkAnswers(char examAnswers[], char inputAnswers[], int numQuestions, int minCorrect) // Function to check exam and user answers.
{
int correctAnswers = 0;
for (int i = 0; i < numQuestions; i++) // Checks the user responses against the correct answers.
{
if (examAnswers[i] == inputAnswers[i])
correctAnswers++;
}
cout << "The list below shows the numbers of the incorrect questions:\n"; // Displays the incorrect question numbers.
for (int i = 0; i < numQuestions; i++)
{
if (examAnswers[i] != inputAnswers[i])
cout << "Question #" << i + 1 << " is incorrect." << endl;
}
}
void displayResult(int correctAnswers)
{
if (correctAnswers >= 15) // Determines if the exam is a pass or fail, displays the results of the exam.
{
cout << "\nYou passed the exam!\n\n";
}
else
{
cout <<"\nYou failed the exam, please come back and test at a later date.\n\n";
}
cout << "Correct Answers = " << correctAnswers << endl; // Shows both the correct and incorrect question count.
cout << "Incorrect Answers = " << 20 - correctAnswers << endl;
cout << "\n";
}
|