0

I'm making a Dice Game in C++. I was wondering why it doesn't restart the loop. The game is Best of 3. It's supposed to restart the loop as long as the player wants to keep playing. However it only restarts the loop once. The second time I press 'Y' or yes in this case it just exits the program.

I've tried putting the restart in a nested while loop but it doesn't seem to work either.

restart:    

while ((pWin != 2) && (cWin != 2))
{
    pDice1 = rand() % 6 + 1;
    cDice1 = rand() % 6 + 1;


    cout << "Player score is: " << pDice1 << endl;
    cout << "Computer score is: " << cDice1 << endl;

    if (cDice1 > pDice1) {
        cout << "Computer wins!" << endl << endl;
        cWin++;

    } if (pDice1 > cDice1) {
        cout << "Player wins!" << endl << endl;
        pWin++;
    } if (pDice1 == cDice1) {
        cout << "It's a draw!" << endl << endl;
    } if (pWin > cWin) {
        cout << "Player wins this round! Do you wish to keep playing?" << endl;
        cin >> Y;
        if (Y == 'y') {
            goto restart;
        }
        else {
            exit(0);
        }
    }if (cWin > pWin) {
        cout << "Computer wins this round! Do you wish to keep playing?" << endl;
        cin >> Y;
        if (Y == 'y') {
            goto restart;
        }
        else {
            exit(0);
        }
    }
        
        

}
xtremesnus
  • 25
  • 5
  • 2
    Using a `goto` to continue a `while` is strange/unnecessary. You can lose the `goto`. Just `exit()` when the user does not enter `y`. Or replace the `goto` with `continue`. – 001 Nov 18 '21 at 18:19
  • 2
    `while ((pWin != 2) && (cWin != 2))` - if you don't want to code to ever stop, don't tell it to stop on some condition. Also please indent your code properly, you've typed your `if`s as if they were an if/else chain. – Mat Nov 18 '21 at 18:19
  • It looks like you want two loops: one that loops until the user doesn't want to play anymore and an inner one to play the best of three games. – 001 Nov 18 '21 at 18:27
  • @Mat I was thinking the goto restart would reset all the conditions of the while loop and restart, maybe that's not how it works? – xtremesnus Nov 18 '21 at 18:32
  • @JohnnyMopp Yes exactly! I was thinking the goto restart would reset all the variables but maybe I should make a nested while loop then? – xtremesnus Nov 18 '21 at 18:33
  • 1
    Here's your code with nested loops and no `goto`. https://onlinegdb.com/zjyCu0Ubx – 001 Nov 18 '21 at 18:38
  • 1
    FYI, the `goto` statement is considered bad practice by many. There are some cases where one could make an argument for their use, but yours is not one of them. [What is wrong with using goto?](https://stackoverflow.com/q/3517726) – 001 Nov 18 '21 at 18:46
  • @JohnnyMopp Great answers! Thanks for that. I'm curious why you wrote "while(1)" What does the 1 mean in this case? – xtremesnus Nov 18 '21 at 19:00
  • 1
    It means "forever" since `1` (or any number that is not `0`) will always be "true". I should have used `while (true)` but I guess I was in `C` mode. So, the loop only ends when the `break` gets hit. – 001 Nov 18 '21 at 19:02

2 Answers2

2

Because you don't reset pWin and cWin to zero after a game.

You should fix that but also turn the goto into another while loop and make the guts of that while loop into a function maybe.

jwezorek
  • 8,592
  • 1
  • 29
  • 46
2

First, is this all your code? I noticed most of your variables seem to be declared outside of the provided code block. If so, is your "Y" being declared as a char and not a string type to match your condition type?

It looks like you are failing to set your pWin and cWin back to zero when it returns to the top. You can fix with:

restart:    
cWin = 0;
pWin = 0;
while ((pWin != 2) && (cWin != 2))
{
pDice1 = rand() % 6 + 1;
cDice1 = rand() % 6 + 1;


cout << "Player score is: " << pDice1 << endl;
cout << "Computer score is: " << cDice1 << endl;

if (cDice1 > pDice1) {
    cout << "Computer wins!" << endl << endl;
    cWin++;

} if (pDice1 > cDice1) {
    cout << "Player wins!" << endl << endl;
    pWin++;
} if (pDice1 == cDice1) {
    cout << "It's a draw!" << endl << endl;
} if (pWin > cWin) {
    cout << "Player wins this round! Do you wish to keep playing?" << endl;
    cin >> Y;
    if (Y == 'y') {
        goto restart;
    }
    else {
        exit(0);
    }
}if (cWin > pWin) {
    cout << "Computer wins this round! Do you wish to keep playing?" << endl;
    cin >> Y;
    if (Y == 'y') {
        goto restart;
    }
    else {
        exit(0);
    }
}
    
    

}
Ryan Millares
  • 525
  • 4
  • 16