So I am trying to build a Rock, Paper, Scissors, Lizard, Spock Game. That basicall prints this:
------------------------------
Enter Move
1 - Rock
2 - Paper
3 - Scissors
4 - Lizard
5 - Spock : 1 // user inputted value
Rock crushes lizard. You win!
------------------------------
Enter Move
1 Rock
2 Paper
3 Scissors
4 Lizard
5 Spock : 40 // user inputted value
Error, invalid input.
------------------------------
Enter Move
1 - Rock
2 - Paper
3 - Scissors
4 - Lizard
5 - Spock : P // user inputted value
Error, invalid input.
----------------------------
and before I start building statements to define the winner of a game, I am trying to ensure that the game does not accept any invalid user inputs. So I got it to accept the proper decimal value, such as 1, and it rejects a number out of range like 40... but when I enter in a character as above say "P" it goes into an infinite loop, and I do not know how to stop this. here is my code:
#include <cstdlib>
#include <iostream>
#include <string>
#include "rpslsType.h"
using namespace std;
int main() {
// stores user input
int u;
// outputs options
cout << "---------------------------" << endl;
cout << "Enter Move" << endl;
cout << "1 - Rock" << endl;
cout << "2 - Paper" << endl;
cout << "3 - Scissors" << endl;
cout << "4 - Lizard" << endl;
cout << "5 - Spock" << " : "; // inputs here
//prompts user input, stores value
cin >> u;
// and when input is not in range of 1 t0 5, print error
while (!((u <= 5) && (u >= 1))) {
printf("Error, invalid input.\n"); // error message
// re-enter user input
// outputs options
cout << "---------------------------" << endl;
cout << "Enter Move" << endl;
cout << "1 - Rock" << endl;
cout << "2 - Paper" << endl;
cout << "3 - Scissors" << endl;
cout << "4 - Lizard" << endl;
cout << "5 - Spock" << " : "; // inputs here
//prompts user input, stores value
cin >> u;
}
return 0;
}
what am I doing wrong?