如何接受 [ENTER] 键作为无效输入并发送错误消息

How to Accept [ENTER] key as an invalid input and send out error message

本文关键字:输入 无效 并发 消息 错误 何接受 ENTER      更新时间:2023-10-16

这是一个对驾驶执照考试问题的用户输入进行评分的程序。 我在验证用户输入时遇到问题。

我想接受 [ENTER] 键作为无效输入并继续我的验证,而不是只是转到空行而无法处理下一个问题。目的是发送错误消息,并且没有给出输入,并且 [ENTER] 键不是有效输入,并且只接受一次输入有效输入的机会,即 a/a、b/b、c/C 或 d/D。所以这就是为什么我在这里使用 if 语句而不是循环。

我尝试了 if (testTakerAnswers[ans] == (or =( ''( {},但仍然没有解决换行符的问题。

我在这里包含curses.h,希望使用另一篇文章中的getch((语句,但不知何故,我无法设法在我的代码中使用数组而不是常规输入。

我也在寻找其他方法而不是 getch((

所以我应该调整我的布尔函数,还是直接验证main((函数中的输入。

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <curses.h>
using namespace std;
const unsigned SIZE = 20;       // Number of qns in the test
char testTakerAnswers[SIZE];    //Array to hold test taker's answers
bool validateInput(char);
class TestGrader
{
private:
char answers[SIZE];        // Holds the correct answers // Answer is array
int getNumWrong (char[]);
void missedQuestions (char[]);
public:
void setKey(string);   // Initialize object with standard keys
void grade(char[]);     // Grades the answers from tester
};
void TestGrader::setKey(string key){
if (key.length()!=SIZE){
cout << "Error in key data.n";
return;
}
for (unsigned pos = 0; pos < SIZE ; pos ++)
answers [pos] = key [pos];
}
void TestGrader::grade(char test[])
{
int numWrong = getNumWrong(test);
if (numWrong <= 5)
cout << "Congratulations. You passed the exam.n";
else
cout << "You did not pass the exam. n";
cout << "You got " << (SIZE-numWrong) << " questions correct. n";
if (numWrong > 0){
cout << "You missed the following " << numWrong << " questions: n";
missedQuestions(test);
}
}
int TestGrader::getNumWrong(char test[])
{
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
counter++;
}
}
return counter;
}
void TestGrader::missedQuestions(char test[])
{
//  cout << testTakerAnswers[i];        This is to print taker's answers
int counter = 0;
for (int i = 0; i < SIZE; i++){
if (answers[i] != toupper(testTakerAnswers[i])){
cout << "n" << i + 1 << ". Correct answers: " << answers[i];
counter++;
}
}
}
bool validateInput(char ans){           // Only A, B, C, D valid input
if (toupper(ans)!='A' && toupper(ans)!= 'B' && toupper(ans)!='C'  && toupper(ans)!= 'D'){
cout << "n********************WARNING*******************n";
cout << "Invalid input! Enter only a/A, b/B, c/C, or d/Dn";
return false;
}
if (testTakerAnswers[ans] == 'n'){
return false;
}
return true;
}
int main()
{
const int NUM_QUESTIONS = 20;
string name;                                 //Test taker's name
char doAnother;                 //Control variable for main processing loop
TestGrader DMVexam;                       //Create a TestGrader object
DMVexam.setKey("BDAACABACDBCDADCCBDA");
do {
cout << "Applicant Name: ";
getline(cin,name);
cout << "Enter answer for " << name << ".n";
cout << "Use only letters a/A, b/B, c/C, and d/D. nn";
for (int i = 0; i < NUM_QUESTIONS; i++){
// Input and validate it
do{
cout << "Q" << i+1 << ": ";
cin >> testTakerAnswers[i];
if (!validateInput(testTakerAnswers[i])){
cout << "You get one more chance to correct.nOtherwise, it count as wrong answer.";
cout << "n*********************************************";
cout << "nRe-enter: ";
cin >> testTakerAnswers[i];
cout << 'n';
break;
}
}while(!validateInput(testTakerAnswers[i]));
}
//Call class function to grade the exam
cout << "Results for " << name << 'n';
DMVexam.grade(testTakerAnswers);
cout << "nGrade another exam (Y/N)? ";
cin >> doAnother;
while (doAnother != 'Y' && doAnother != 'N' && doAnother != 'y' && doAnother != 'n'){
cout << doAnother << " is not a valid option. Try Again y/Y or n/N" << endl;
cin >> doAnother;}
cout << endl;
cin.ignore();
}while(doAnother != 'N' && doAnother != 'n');
return 0;
}

您的问题是cin >> testTakerAnswers[i];cin 是空格分隔的,这意味着任何空格(包括""(都将被丢弃。所以testTakerAnswers[i]永远不能是''。

我不确定你想做什么,但可以尝试

getline(cin,input_string);

然后

input_string == "A" | input_string == "B" | ...

因此,如果只按回车键,input_string将变为"".