How to make it so you HAVE to insert the choices given?


So the player needs to say whether they will attack "head" or "body" how would I do this?
Like you can type head or body now, but i want it so you HAVE to choose those options or else it will give you back a retry statement.
choice 1 is where one player attacks and choice 2 is where one player defends. both are strings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 cout << player1 << ", where do you strike?" << endl << endl;
			cin >> choice1;
			cout << string(50, '\n');
			
			cout << player2 << ", where do you defend?" << endl << endl;
			cin >> choice2;
			cout << string(50, '\n');


			if (choice1 != choice2)
			{
				critChance = rand() % 100;

				cout << player1 << "'s attack is successful!" << endl;
				player2Hp = player2Hp - 1;

				// 10% CHANCE TO CRIT***********
				if (critChance < 15)
				{
					player2Hp = player2Hp - 1;
					cout << endl << player1 << " has landed a Crit!" << endl << endl;
				}
				//*****************************
				if (player2Hp > 0)
				{
					cout << player2 << "'s HP is now down to: " << player2Hp << endl << endl;
				}
				else
				{
					cout << player2 << " is bleeding out!" << endl << endl;
Last edited on
1
2
3
4
5
6
cout << player1 << ", where do you strike?" << endl << endl;
cin >> choice1;
while (choice1 != "head" && choice1 != "body") {
    cout << "Wrong input, try again: ";
    cin >> choice1;
}; 


Or you can make a string with possible answers as a substrings and then you can check if user's answer is inside that string (if it is, then user's input is correct).

1
2
3
4
5
6
7
string possibleAns = "head body legs arms";
cout << player1 << ", where do you strike?" << endl << endl;
cin >> choice1;
while (possibleAns.find(choice1) == string::npos) {
    cout << "Wrong input, try again: ";
    cin >> choice1;
}; 


s1.find(s2) will return (std::)string::npos if s2 isn't inside s1.
http://www.cplusplus.com/reference/string/string/find/
Last edited on
Topic archived. No new replies allowed.