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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Madeline Schimenti
// Summer 2014
// Chapter 6 & 7 Program
#include <iostream>
using namespace std;
#include <cctype>
int main()
{
// Constants and variables
const int NUM_QUESTIONS = 20;
const int MIN_CORRECT = 15;
char answers[NUM_QUESTIONS] = {
'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A'
};
char a = 'A';
a=toupper(a);
char b = 'B';
b=toupper(b);
char c = 'C';
c=toupper(c);
char d = 'D';
d=toupper(d);
// Array for input
char studentAnswers[NUM_QUESTIONS];
void inputAnswers(char[], int);
void gradeAnswers(char[], char[], int, int);
void displayAnswers(char[], char[], int);
cout<< "Welcome to the Driver's License Test! \n";
cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
// call
inputAnswers(answers, NUM_QUESTIONS);
gradeAnswers(answers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
displayAnswers(studentAnswers, answers, NUM_QUESTIONS);
return 0;
}
// INPUT FUNCTION
void inputAnswers(char studentAnswers[], int NUM_QUESTIONS)
{
for (int index = 0; index < NUM_QUESTIONS; index++)
{
cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
cin >> studentAnswers[index];
//Input validation of users answers
while (studentAnswers[index] != 'A' && studentAnswers[index] != 'a' && studentAnswers[index] != 'B' && studentAnswers[index] != 'b' && studentAnswers[index] != 'C' && studentAnswers[index] != 'c' && studentAnswers[index] != 'D' && studentAnswers[index] != 'd')
{
cout << "You must enter A, B, C, or D\n";
cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
cin >> studentAnswers[index];
}
}
}
// Function gradeAnswers
void gradeAnswers(char CORRECTanswer[], char student_input_answer[], int NUM_QUESTIONS, int MIN_CORRECT)
{
int correctAnswers = 0;
cout << "\nYou must have at least 15 correct to pass.";
// Grade each answer
for (int index = 0; index < NUM_QUESTIONS; index++)
{
using namespace std;
if (student_input_answer[index] == (CORRECTanswer[index]))
correctAnswers++;
}
if (correctAnswers >= MIN_CORRECT)
{
cout << "\nCongratulations! You passed the exam!\n\n";
}
else
{
cout << "\nSorry, you did not pass the exam." << endl;
}
}
// Display Function
void displayAnswers(char student_input_answers[], char CORRECTanswer[], int NUM_QUESTIONS)
{
cout << "\n\nIncorrect Answers:\n"
<< " -----------------";
for ( int index = 0; index < NUM_QUESTIONS; index++)
{
if (student_input_answers[index] != CORRECTanswer[index])
cout << "\n" << index + 1 << "." << student_input_answers[index];
}
}
|