kelas3
kelas3

Reputation: 23

How would I avoid using a goto statement here?

I am pretty new to programming in C and I am making a simple dice based game. I have all the other logic working but I am having trouble seeing how to avoid a goto statement. Basically the player starts with 50 dollars as their balance and the program asks for a bet, then dice are rolled and depending on what the result of the roll is different things happen. The main issue is when the result happens the user is asked if they want to play again, if they say no then I just exit but if they say yes I need to go back to the start and ask for a bet again.


int main()
{

        START:

    if (FirstRun == 0)
    {
        printf("Your starting balance is $%.2f", Balance);
    }
    if (FirstRun > 0)
    {
        printf("Your new balance is $%.2f", Balance);
    }
    if (Balance <= 0)
    {
        printf("Sorry you are out of money!");
        exit(0);
    }

    do
    {
        ValidBet = 0;
        printf("\nPlease enter your bet:");
        scanf("%lf", &Bet);

        if ((Bet > Balance) || (Bet < 0))
        {
            printf("Your bet is invalid, try again.");
        }
        else 
        {
            ValidBet = 1;
        }
    } while (ValidBet == 0);

    ThrowDicePair();
    printf("DICE #1 WAS %d", SumOfThrows);

    if (SumOfThrows == 7 || SumOfThrows == 11)
    {
        Balance += Bet;
        printf("You win! Would you like to play again? [y/n]");
        C = getch();
        if (C == 'y')
        {
            FirstRun++;
            goto START;
        }
        else if (C == 'n')
        {
            printf("Thanks for playing");
            exit(0);
        }

}

Upvotes: 2

Views: 123

Answers (3)

birneee
birneee

Reputation: 663

int main() {
    while (1) { /* ADDED  */
        if (FirstRun == 0) {
            printf("Your starting balance is $%.2f", Balance);
        }
        if (FirstRun > 0) {
            printf("Your new balance is $%.2f", Balance);
        }
        if (Balance <= 0) {
            printf("Sorry you are out of money!");
            exit(0);
        }

        do {
            ValidBet = 0;
            printf("\nPlease enter your bet:");
            scanf("%lf", &Bet);

            if ((Bet > Balance) || (Bet < 0)) {
                printf("Your bet is invalid, try again.");
            } else {
                ValidBet = 1;
            }
        } while (ValidBet == 0);

        ThrowDicePair();
        printf("DICE #1 WAS %d", SumOfThrows);

        if (SumOfThrows == 7 || SumOfThrows == 11) {
            Balance += Bet;
            printf("You win! Would you like to play again? [y/n]");
            C = getch();
            if (C == 'y') {
                FirstRun++;
                continue; /* ADDED  */
            } else if (C == 'n') {
                printf("Thanks for playing");
                exit(0);
            }
        }
        break; /* ADDED  */
    }
}

Upvotes: 0

bruno
bruno

Reputation: 32594

just put all in a for(;;) loop; the goto will be the case where you reloop, in the other cases add a return 0;(or exit(0); as you prefer and already use in some cases)

int main()
{

  for (;;) { /* ADDED  */
    if (FirstRun == 0)
    {
        printf("Your starting balance is $%.2f", Balance);
    }
    if (FirstRun > 0)
    {
        printf("Your new balance is $%.2f", Balance);
    }
    if (Balance <= 0)
    {
        printf("Sorry you are out of money!");
        exit(0);
    }

    do
    {
        ValidBet = 0;
        printf("\nPlease enter your bet:");
        scanf("%lf", &Bet);

        if ((Bet > Balance) || (Bet < 0))
        {
            printf("Your bet is invalid, try again.");
        }
        else 
        {
            ValidBet = 1;
        }
    } while (ValidBet == 0);

    ThrowDicePair();
    printf("DICE #1 WAS %d", SumOfThrows);

    if (SumOfThrows == 7 || SumOfThrows == 11)
    {
        Balance += Bet;
        printf("You win! Would you like to play again? [y/n]");
        C = getch();
        if (C == 'y')
        {
            FirstRun++;
            /* goto START; removed */
        }
        else if (C == 'n')
        {
            printf("Thanks for playing");
            exit(0);
        }
        else /* ADDED */
          return 0; /* ADDED */
    }
    else /* ADDED */
      return 0; /* ADDED */
  } /* ADDED  */
}

In more complicated/nested cases you can use a continue to reloop, but here this is useless

Upvotes: 3

CodeoftheWarrior
CodeoftheWarrior

Reputation: 363

Assuming you want to ensure the person plays the game at least one time before electing to quit, a do...while loop would work just fine. Just start the loop where you want to iterate back to, i.e. your START label and then take the input into C at the end of your loop conditioning on whether or not your input is equal to 'n' or 'N'.

Upvotes: 2

Related Questions