Question
Validating input under while ((input < 'A') || (input > 'D')), get caught in loop. Here is full file. /******************** Drivers License ********************/ #include #include
Validating input under while ((input < 'A') || (input > 'D')), get caught in loop. Here is full file.
/********************
Drivers License
********************/
#include
#include
#include
using namespace std;
// Class
class TestGrader
{
char answers[20];
int ans[20];
public:
// Functions
void setKey(char Key[]);
void grade(char Answers[]);
// void numToString(int num[]);
// void numToString(char cString[]);
// void numToString(string num[]);
};
// Member function of setKey
void TestGrader::setKey(char Key[])
{
for (int i = 0; i < 20; i++)
{
if (Key[i] >= 'A'&&Key[i] < 'D')
answers[i] = Key[i];
else
answers[i] = ' ';
}
return;
}
// Member function of grade
void TestGrader::grade(char testAnswer[])
{
int correct = 0;
for (int i = 0; i < 20; i++)
{
if (testAnswer[i] == answers[i] || answers[i] == ' ')
{
correct++;
}
}
// Check answers
if (correct >= 15)
{
cout << " The applicant has passed the exam. ";
}
else
cout << " The applicant has failed the exam. ";
cout << "Number of correct answers: " << correct << endl;
cout << "Number of incorrect answers: " << 20 - correct << endl;
cout << " List of questions with incorrect answers: ";
for (int i = 0; i < 20; i++)
{
if (testAnswer[i] != answers[i] && answers[i] != ' ')
cout << i+1 << " ";
}
}
/***************************************************
// Member funciont of numToString
string TestGrader::numToString(int num[]) const
{
ostringstream ss;
ss << num;
return ss.str();
}
// Member function of numToString
string TestGrader::numToString(int num) const
{
char cString[3];
_itoa_s(num, cString, 10);
string numString(cString);
return numString;
}
// Member functin of numToSTring
string TestGrader::numToString(int num) const
{
string numString = std::to_string(num);
return numString;
}
**************************************************/
int main()
{
// Declare variables
char input, answers[20];
// Define array of answers
char correctAnswers[20] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
// TestGrader class object
TestGrader Test;
// Call the setKey function
Test.setKey(correctAnswers);
do
{
// Input
cout << "Welcome to Driver's License Exam";
cout<<" There are 20 multiple choice questions ranging from A to D.";
for (int i = 0; i < 20; i++)
{
cout << " Enter an answer to Question " << i + 1 << ": ";
cin >> input;
input = toupper(input);
while ((input < 'A') || (input > 'D'))
{
cout << " Invalid input, try again.";
cout << " Enter an answer to Question " << i + 1 << ": ";
cin >> input;
}
if (input >= 'A' && input <= 'D')
answers[i] = input;
}
// Call the grade function
Test.grade(answers);
// Allow more test
cout << " Do you want to continue?";
cout << " Press Y to continue or Q to quit";
cin >> input;
}while (input != 'Q' && input != 'q');
cin.get();
return 0;
}
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