Benutzereingabe von Ganzzahlen - Fehlerbehandlung

Ich habe Probleme mit bestimmten Eingabebereichen meines Programms. Es gibt einige Bereiche, in denen der Benutzer eine bestimmte Ganzzahl eingibt. Selbst wenn sie das falsche eingeben, das alles in Ordnung und gut ist, aber ich habe bemerkt, dass es die Fehlermeldung wiederholt wiederholt, wenn sie etwas eingeben, das nicht ganzzahlig ist wie 'm'.

Ich habe ein paar Funktionen, die Integer-Eingabe in ihnen haben. Hier ist ein Beispiel.

void Room::move(vector<Room>& v, int exone, int extwo, int exthree, int current)
{
    v[current].is_occupied = false;
    int room_choice;
    cout << "\nEnter room to move to: ";
    while(true)
    {
        cin >> room_choice;
        if(room_choice == exone || room_choice == extwo || room_choice == exthree)
        {
            v[room_choice].is_occupied = true;
            break;
        }
        else cout << "Incorrect entry. Try again: ";
    }
}

[Gelöst]

void Room::move(vector<Room>& v, int exone, int extwo, int exthree, int current)
{
    v[current].is_occupied = false;
    int room_choice;
    cout << "\nEnter room to move to: ";
    while(true)
    {
        cin >> room_choice;
        if(room_choice == exone || room_choice == extwo || room_choice == exthree)
        {
            v[room_choice].is_occupied = true;
            break;
        }
        else if(cin.fail())
        {
          cin.clear()
          cin.ignore()
          cout << "Incorrect entry. Try again: ";
        }
    }
}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage