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 82 83 84 85 86 87 88
|
#include <iostream>
#include <cctype>
using namespace std; // <--- Best not to use.
void grade(char[], char[], int, int);
int main()
{
const int NUM_QUESTIONS = 10;
const int MIN_CORRECT = 8;
constexpr int MAX_TRIES{ 3 };
char correctAnswers[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
char studentAnswers[NUM_QUESTIONS];
int numTries{};
//Loop for users answers
for (int replies = 0; replies < NUM_QUESTIONS; replies++)
{
if (numTries == 3) break;
cout << "Please enter your answers: "
<< (replies + 1) << ": ";
cin >> studentAnswers[replies];
studentAnswers[replies] = static_cast<char>(std::toupper(static_cast<unsigned char>(studentAnswers[replies])));
//Validation of users answers
//while (studentAnswers[replies] != 'A' && studentAnswers[replies] != 'B' && studentAnswers[replies] != 'C' && studentAnswers[replies] != 'D')
while ((studentAnswers[replies] < 'A' || studentAnswers[replies] > 'D') && numTries < MAX_TRIES)
{
cout << "\n You must enter A, B, C, or D\n\n";
numTries++;
cout << "Please enter your answers: " << (replies + 1) << ": ";
cin >> studentAnswers[replies];
}
}
if (numTries<MAX_TRIES)
{
grade(correctAnswers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
}
else
{
std::cout << "\n Your good bye message!\n";
}
return 0;
}
void grade(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT)
{
//cout << "max: " << NUM_QUESTIONS;
int correctAnswers = 0;
//Check the student's replies against the correct answers
for (int i = 0; i < NUM_QUESTIONS; i++)
{
if (answers1[i] == stu_answers1[i])
correctAnswers++;
}
//Did they pass or fail?
if (correctAnswers >= MIN_CORRECT)
{
cout << "\nCongratulations!\n"
<< "You have passed the exam!\n\n";
}
else
{
cout << "\nSorry, you have not passed the exam!\n\n";
}
//Display a list of the questions that were incorrectly answered.
cout << "The list below shows the question numbers of the incorrectly";
cout << " answered questions.\n";
for (int i = 0; i < NUM_QUESTIONS; i++)
{
if (answers1[i] != stu_answers1[i])
cout << "Question # " << i << " is incorrect." << endl;
}
//Display the number of correct and incorrect answers provided by the student.
cout << "\nCorrect Answers = " << correctAnswers << endl;
cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << endl;
}
|