处理C++中的多个字符输入问题

问题描述 投票:0回答:1

我目前正在开发一个 C++ 测验程序,其中有两个函数:

ProcessQuestion
DisplayQuestions
ProcessQuestion
函数负责显示问题及其选项、提示用户答案、验证输入并检查答案是否正确。另一方面,
DisplayQuestions
功能可以协调多个问题的显示。

但是,我在

ProcessQuestion
函数中遇到用户输入处理问题。当用户输入多个字符作为答案时(例如“cd”),程序似乎将第一个字符作为当前问题的答案,但随后的字符将被视为下一个问题的答案,而不会再次提示用户。

    void DisplayQuestions()
    {
        system("cls");
        CentredMessage("Quiz Start");

        cout << endl << endl;
        int questionIndices[Q]; // Array to store question indices
        for (int i = 0; i < Q; ++i) {
            questionIndices[i] = i; // Fill array with question indices
        }
        srand(static_cast<unsigned int>(time(0))); // Seed for random number generation

        // Shuffle question indices using rand()
        for (int i = Q - 1; i > 0; --i) {
            int j = rand() % (i + 1); // Generate a random index
            // Swap questionIndices[i] and questionIndices[j]
            int temp = questionIndices[i];
            questionIndices[i] = questionIndices[j];
            questionIndices[j] = temp;
        }

        // Display questions
        for (int i = 0; i < Q; ++i) {
            ProcessQuestion(questionIndices[i], i + 1);
            cout << endl;
        }

    }


    void ProcessQuestion(int questionIndex, int Qno)
    {
        cout << "Question " << Qno << ": " << Question[questionIndex] << endl;
        for (int i = 0; i < O; ++i)
        {
            cout << Option[questionIndex][i] << endl;
        }

        cout << "Enter your answer: ";
        char userAnswer;
        cin >> userAnswer;
        while (!(userAnswer >= 'A' && userAnswer <= 'D') && !(userAnswer >= 'a'))
        {
            cout << "Invalid Option.\n";
            cout << "Enter your answer Again: ";
            cin >> userAnswer;
        }

        userAnswer = toupper(userAnswer);

        string correctAnswer = Correct[questionIndex][0];
        if (userAnswer == correctAnswer[0])
        {
            cout << "Correct answer!" << endl;
            SCORE_ = SCORE_ + 2;
            CorrectAnswerCount_++;
        }
        else {
            cout << "Incorrect answer.";
            cout << " The correct answer is: " << correctAnswer << endl;
            WrongAnswerCount_++;
        }

        system("pause");
        system("cls");
    }

当调用 ProcessQuestion 时,它会显示问题和该问题选择,然后询问答案,在答案输入中,如果我输入两个字符(例如 cd),它将采用第一个字符作为当前问题的答案,然后执行其余代码,但是当为下一个问题调用 ProcessQuestion 时,它会采用下一个字符 d 作为第二个问题的答案,并且没有向用户询问

输出

Quiz Start


Question 1: What is the purpose of the 'using namespace std;' statement in C++?
A: It imports the standard input/output library
B: It defines the standard namespace for C++ programs
C: It allows you to use functions and objects from the standard namespace without specifying std::
D: It is not a valid C++ statement
Enter your answer: cd
Correct answer!

Question 2: What is the purpose of the 'return' statement in C++?
A: It starts a new function
B: It exits a loop
C: It terminates the program
D: It returns a value from a function
Enter your answer: Correct answer!

我尝试通过添加

cin.ignore(10000000000000000000, '\n');
来解决该问题,以在阅读用户的答案后清除输入缓冲区。 最初,这似乎解决了问题,但令人费解的是,它已经停止工作了。

void ProcesssQuestion(int questionIndex, int Qno) {

    char userAnswer;
    cin >> userAnswer;
    cin.ignore(10000000000000000000, '\n');

}
c++ character
1个回答
0
投票

您从用户处获取输入并将其存储到

char
此数据类型只有一个字符,因此每次输入时它仅从输入流中读取一个字符。

我建议将整个输入读入

string
,例如:

string users;
cin >> users;

然后您可以检查他们实际上输入了任何内容(不仅仅是按下回车键),以及他们是否只使用了第一个字符:

char choice = users[0];
© www.soinside.com 2019 - 2024. All rights reserved.