如何结束这个功能? (C++)

How to end this function? (C++)

我一直在为我的学期作业做一个测验。这很简单,因为我是初学者。而且我想让用户插入选项的字符但是插入字符后它并没有停止,我不知道如何解决它。

int giveAnswera (string answer)
{
    int x = 0;
    cout << "Enter the answer in form of a, b or c." << endl;
    cin >> answer;
    if (cin >> answer == "a")
    {
        cout << "✓" << endl; 
        cout << "Well done." << endl;
        x = x+2;
    }
    else 
    {
        cout << "×" << endl;
        cout << "You're wrong. You get no points." << endl;
        x = x+0; 
    }
 return x;
}

在 C++11 中,它无法编译。

在 C++03 中,它 确实 编译,但是您尝试使用 cin >> answer 读取两次,并且该函数在等待第二次输入时卡住。
条件应该只是 answer == "a".

并且由于您没有将函数参数的值用于任何用途,因此您应该将其删除并改用局部变量:

int giveAnswera ()
{
    string answer;
    int x = 0;
    cout << "Enter the answer in form of a, b or c." << endl;
    cin >> answer;
    if (answer == "a")
    {
        cout << "✓" << endl; 
        cout << "Well done." << endl;
        x = x+2;
    }
    else 
    {
        cout << "×" << endl;
        cout << "You're wrong. You get no points." << endl;
    }
    return x;
}

你不应该使用

if(cin >> answer == "a")

改为使用

cin >> answer;
if(answer == "a")