Tuesday, October 28, 2014

Drivers licence exam C++

/////////////////////////////////////////////////////////////////////////////////////////////////
// Author:Joe Student                                               //
//                                                                                                      //
// Date:                    09/06/14    34                                                  //
// Program Description:Write a program that grades the written   //
//portion of the driver's license exam.                                            //
//                                                                                                     //
// Input:  Answers (A,B,C,D)                                                        //
// Processing:   Compared to prepared answer bank                     //
// Output: Number wrong, number right , pass/fail                       //
///////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <cctype>
using namespace std;
void checkAnswers(char[], char[], int, int);
int main() {
    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 stu_answers[NUM_QUESTIONS];
    //Loop for users answers
    for (int replies = 0; replies < NUM_QUESTIONS; replies++) {
        cout<< "Please enter your answers (Hint: Use capital letters): "
            << (replies + 1) << ": ";
        cin >> stu_answers[replies];
        //Validation of users answers
        while (stu_answers[replies] != 'A' && stu_answers[replies] != 'B' && stu_answers[replies] != 'C' && stu_answers[replies] != 'D') {
            cout << "You must enter A, B, C, or D\n";
            cout<< "Please enter your answers (Hint: Use capital letters): "
                << (replies + 1) << ": ";
            cin >> stu_answers[replies];
        }
    }
    checkAnswers(answers, stu_answers, NUM_QUESTIONS, MIN_CORRECT);
    return 0;
}
void checkAnswers(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?
    cout << "\n\n\n\n\n\n\n\n\nYou must have at least 15 correct to pass.";
    cout << "\n-------------------------------------------";

    if (correctAnswers >= MIN_CORRECT) {
        cout << "\nStudent passed the exam";
       cout << "\n---------------------------\n\n\n\n\n";

    }
    else {
        cout <<"\nStudent failed the exam";
       cout << "\n---------------------------\n\n\n\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;
}

No comments:

Post a Comment