C++代码buggy,需要修复

-4 c++ validation input

嘿伙计们,我有这段代码,发布在下面.我添加了while循环以确保只使用数字输入,但是当我使用它时,它需要我输入两次数字,或按Enter键然后输入数字.

输出将是:

Input number : 1
1
Run Code Online (Sandbox Code Playgroud)

然后它会打印结果.我怎样才能解决这个干杯问题.

 void Dictionary::SearchNumeric()
{
    int x;
    cout << "Input number : ";
    while (!(cin >> x))
    {
        cout << "Invalid input.  Try again: ";
        cin.ignore(numeric_limits<streamsize>::max());

    }
    string searchWord = myWords[x]->word;
    cout << "Word searched: " << searchWord << endl;
    cout << "Definition: \n" << myWords[x]->definition << endl;
    cout << "Type: " << myWords[x]->type << endl;
    int wordIndex = 0;
    //while (myWords[wordIndex]->word.compare(x) != 0) {
    //needs to return scrabble score
    wordIndex++;
    //break;
    //}

}
Run Code Online (Sandbox Code Playgroud)

小智 5

摆脱第一个cin >> x;,设置string searchWord一会儿