Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Validating input under while ((input < \'A\') || (input > \'D\')), get caught in

ID: 3853460 • Letter: V

Question

Validating input under while ((input < 'A') || (input > 'D')), get caught in loop. Here is full file. Previous post had following if statement put within the while braces, but still loops.

#include<iostream>

#include<string>
#include<sstream>
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')) //Changes in the code as per requirements
{
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;
}

Explanation / Answer

You can just use this code by removing while loop and 3 lines above if condition

if (input >= 'A' && input <= 'D')
   answers[i] = input;
else
{
   cout << " Invalid input, try again.";
   i -= 1;
}

I have highlighted the modified code in the following full code.

#include<iostream>
#include<string>
#include<sstream>
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')) //Changes in the code as per requirements
//{
//cout << " Invalid input, try again.";
//cout << " Enter an answer to Question " << i + 1 << ": ";
//cin >> input;
if (input >= 'A' && input <= 'D')
   answers[i] = input;
else
{
   cout << " Invalid input, try again.";
   i -= 1;
}
//}

}

// 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;
}