While loop help

Ignore the extra things in the header, as this is only a fragment of my code.
I am trying to create a game of rock paper scissors and I NEED have the input as a string. Currently, I am having issues repeating the program. Should the user place an invalid input, the program should prompt the user to reenter a selection. When there is a bad input, the program will just continue on if any key is pressed. What can I do to solve this problem?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>

using namespace std;

int main()
{   
    cout << "Welcome to the battleground, choose your weapon." << endl; 
    cout << "Enter rock, paper, scissors, or quit to exit." << endl;
    
    string rpsChoice;
    
    do{
    getline(cin, rpsChoice, '\n');
    
    if (rpsChoice == "rock")
    {
        cout << "You have chosen rock as your weapon, a wise decision." << endl;
        int rpsPlayer = 0;
    }
    else if (rpsChoice == "scissors")
    {
        cout << "You have chosen scissors as your weapon, clever." << endl;
        int rpsPlayer = 1;
    }
    else if (rpsChoice == "paper")
    {
        cout << "You have chosen paper as your weapon, fool." << endl;
        int rpsPlayer = 2;
    }
    else if (rpsChoice == "quit")
    {
        return 0;
    }
    else
    {
        cout << "INVALID WEAPON! Do you want to die???" << endl;
        cout << "Enter rock, paper, or scissors now!" << endl;
        break;
    }
    }while ((rpsChoice != "rock")&&(rpsChoice != "paper")&&(rpsChoice != "scissors")
&&(rpsChoice != "quit"));
    system("pause");
    
Last edited on
Your problem is the use of the break keyword (line 41 of your example). What break does is pretty close to what the name implies - it breaks execution out of the loop and continues to the next statement in your program, which in this case is system("pause").

The do-while loop itself looks to be fine. If you remove the break statement it should do what you expect. However, I suggest that you declare a bool outside your loop and use it to keep track of whether or not the user made a valid input. Then, all you have to do in your while statement is check whether that bool is true:

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
31
32
33
34
35
36
37
...

bool isValid = false;
string rpsChoice;

do
{
    getline(cin, rpsChoice, '\n');

    if (rpsChoice == "rock")
    {
        cout << "You have chosen rock as your weapon, a wise decision." << endl;
        int rpsPlayer = 0;
        isValid = true;
    }
    else if (rpsChoice == "scissors")
    {
        cout << "You have chosen scissors as your weapon, clever." << endl;
        int rpsPlayer = 1;
        isValid = true;
    }
    else if (rpsChoice == "paper")
    {
        cout << "You have chosen paper as your weapon, fool." << endl;
        int rpsPlayer = 2;
        isValid = true;
    }
    else if (rpsChoice == "quit")
    {
        return 0;
    }
    else
    {
        cout << "INVALID WEAPON! Do you want to die???" << endl;
        cout << "Enter rock, paper, or scissors now!" << endl;
    }
} while (isValid != true);
Thanks much for the help :)
Topic archived. No new replies allowed.