C++代码错误,需要修复

C++ code buggy, needs fixing

本文关键字:代码 错误 C++      更新时间:2023-10-16

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

输出将是:

Input number : 1
1

然后它将打印结果。我该如何解决这个问题 干杯.

 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;
    //}
}

去掉第一个cin >> x;,在 while 之后设置string searchWord