Code:
// Madeline Schimenti
// Summer 2014
// Chapter 6 & 7 Program
#include <iostream>
using namespace std;
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'
};
// 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])
// {int lower_int = CORRECTanswer[index] - 32;
if ((CORRECTanswer[index] || lower_int) == (student_input_answer[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];
}
}