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

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

这是一个对用户输入的驾照考试问题进行评分的程序。 我无法验证用户输入。

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

我试了 if (testTakerAnswers[ans] == (or =) '\n') {} 还是没有解决换行问题

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

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

所以我应该调整我的 bool 函数,还是直接在 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/D\n";
        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. \n\n";

        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 是用空格分隔的,这意味着任何空格(包括'\n')都将被丢弃。所以 testTakerAnswers[i] 永远不能是 '\n'.

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

getline(cin,input_string);

然后

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

所以如果只按回车键,input_string会变成""