Question
Hi this is a problem from the book starting out with c++ from Tony Gaddis!!! chapter 7 problem # 10. Driver's License exam. One of
Hi this is a problem from the book starting out with c++ from Tony Gaddis!!! chapter 7 problem # 10. Driver's License exam. One of my function that I created to keep track of incorrectly answerd question by the user is not functionaning properly. Name of the fucntion is ((int correctAnswerEnteredByTheUser(). It is very last function in my solution and I cannot figure it out why. The counter is not counting as it is suppose to.
Thank you!!
//*************************************************************************************************************************************************************************************
/*
Name: Charles Dhital
Date: 01/19/2018
Program: Driver's License Exam.
Program Description:
*/
#include
// file includes the declarations of the basic standard input-output library in c++
#include
using namespace std; // all the elements library are decleared within the namespace, the namespace with the name std. In order to acess its functionality
// we declear with this expression that we will be using these entities.
//*****************************************************************************************************
// Function prototype to see if user enters anything other than A, B, C or D as choice of their answer
//******************************************************************************************************
bool validateUserAnswer(char);
//********************************************************************
// Function prototype to evalute the correct answer enter by the user.
//*********************************************************************
int correctAnswerEnteredByTheUser();
const char RIGHT_ANSWER = 20; // this is the constant for the answer that is correct. It holds 20 elements.
char rightAnswers[RIGHT_ANSWER] = { 'A', 'D', 'B', 'B', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'C', 'D', 'B', 'D', 'C', 'C', 'A', 'D', 'B' };
// this // variable rightAnswer holds 20 elements of character.
const char USER_ANSWER = 20; // this constant has 20 character elements.
char userAnswer[USER_ANSWER]; // this variable, userAnswer will hold 20 answer entered by the user.
const int PASSING_GRADE = 15; // this variable is holding the number of answers that user needs to answer to pass the test.
int main() // the line correspond to the beginning of the defination of the main function. The main function is the point where all c++ program starts
// their execution.
{
// Prompt instruction to user.
cout << "\t --------------------------------------------------------------------" << endl;
cout << "\t Please answer these question. Only Enter character A, B, C, or D" << endl;
cout << "\t----------------------------------------------------------------------" << endl;
int correctAnswer = 0; // this variable will call the function correct answer function will will evalute correct answer entered by the
// user.
// Prompt question on the screen for the user using a loop.
for (int i = 0; i < RIGHT_ANSWER; i++)
{
do
{
cout << " Enter the answer for question # " << (i + 1) << endl;
cin >> userAnswer[i];
} while (!validateUserAnswer(userAnswer[i])); // here function validateUserAnswer is being called for the while statement. To check
// if user inputs the valid choice (A, B, C and D).
}
correctAnswer = correctAnswerEnteredByTheUser(); // here correctAnswer variable is calling the function correct answer enterd by the
// user.
if (correctAnswer >= PASSING_GRADE)
{
cout << " You passed the test!" << endl;
}
else
{
cout << " You failed the test. Good luck next time!" << endl;
}
cout << " Total number of correctly answered questions: " << correctAnswer;
_getch();
return 0; // return statement causes the main function to finish.
}
bool validateUserAnswer(char answer) // this function will be used to check user input, if user inputs invalid input this function will
// return false and if user inputs valid answer within the parameter it will return true.
{
if (answer != 'A' && answer != 'B' && answer != 'C' && answer !='D' && answer != 'a' && answer != 'b'
&& answer != 'c' && answer != 'd')
{
cout << " Invalid input, only enter A, B, C or D for the answer" << endl;
return false;
}
return true;
}
int correctAnswerEnteredByTheUser()
{
int counter =0; // this variable will keep the count of the correct answer when user enteres the correct answer.
for (int i = 0; i < RIGHT_ANSWER; i++)
{
if (rightAnswers[i] == userAnswer[i]) // here if correct answer that is stored is == answer entered by the
// user that counter will be incereased if not it will not increase.
counter++;
}
return counter;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started