它适用于任何数字输入,但如果您输入一个字母或单词,它会永远循环错误消息。如何解决这个问题?
while(choice != 2){
cout << "Player stats:\n Level ------ " << level << "\n Health ----- " << health << "\n Attack ----- " << attack << "\n Experience - " << exp << endl;
cout << " " << endl;
cout << "-Choose an option-" << endl;
cout << "1| Fight | Fight a monster | Monsters are based on your level" << endl;
cout << "2| Exit | Exit the game" << endl;
currentHealth = health;
cin.clear();
cin >> choice;
while(choice < 1 || choice > 2){
cout << "Invalid choice! Try again!" << endl;
cin >> choice;
}