-2

I am writing a program in C that plays the game Craps. After my first roll, when I either land the point (win) or lose, the program just ends instead of calling is_win or is_loss.

But if it calls is_win or is_loss on my first roll, everything works fine.

I'm fairly certain this has something to do with the pointer pbal. I've been getting errors in the debugger saying unable to read memory, also Exception Thrown: Read access violation. I am still fairly new to pointers so I'm assuming this is the root cause of the problem.

# include "header.h"

int print_menu(void) {
    int choice = 0;
    printf("\n1. Enter balance\n2. Play game\n3. Get rules\n4. End game");
    printf("\nEnter which you would like to do: ");
    scanf("%d", &choice);
    return choice;
}

void print_rules(void) { 
    printf("\nFirst Roll: If your die roll adds to 7 or 11, you win.\nIf it adds to 2, 3, or 12, you immediately lose.\nIf it adds to another number, that number becomes your 'point'");
    printf("\nSecond Roll: If your die roll adds to your point, you win. If it adds to 7, you lose.\nKeep rolling until you get one of these.\n\n");
}

int get_balance(balance) {

    printf("\nEnter how much money you would like to add (whole dollars): ");
    scanf("%d", &balance);
    return balance;
}

int prompt_bet(balance) {
    int bet = 0;
    do {
        printf("\nEnter how much you would like to bet for this game: ");
        scanf("%d", &bet);
        } while (bet > balance); //repeats until its false that bet>bal
    return bet;
}

int roll_dice(void) {
    srand((unsigned int)time(NULL));

    int enter = 1;
    printf("\nEnter a 0 to roll your dice: ");
    scanf("%d", &enter);

    int die1 = rand() % 6 + 1;
    int die2 = rand() % 6 + 1;
    int dice = die1 + die2;
    printf("You rolled a %d and a %d, with a total of %d.\n", die1, die2, dice);
    return dice;
}

int calc_first_roll(int dice, int bet, int balance, int * pbal) {
    int result0 = 0;

    if (dice == 7 || dice == 11) {
        is_win(bet, balance, pbal);
        }

    else if (dice == 2 || dice == 3 || dice == 12) {
        is_loss(bet, balance, pbal);
        }

    else {

        printf("Your point is %d", dice);
        int point = dice;

        int done = 1;
        do {
            dice = roll_dice();
            done = calc_other_rolls(point, dice, balance, bet, *pbal);


        } while (!done);


        }

    return result0;
}

void is_win(int bet, int balance, int *pbal) {

    /* the pointer *pbal is pointing to mainbalance. I had to pass it
    through everything to get it to affect mainbal the way I wanted it to.
    Think of mainbalance as the permanent memory for keeping their bets & money right,
    and int balance is just a messenger that doesn't get in the way of me trying
    to use a pointer on mainbalance. */

    *pbal = balance + bet;
    printf("You win! Your new balance is %u\n", *pbal);
}

void is_loss(int bet, int balance, int *pbal) {
    *pbal = balance - bet;
    printf("You lost. Your new balance is %u\n", *pbal);
}

int calc_other_rolls(int point, int dice, int balance, int bet, int *pbal) {
    int done = 0;
    if (dice == 7) {  //Goes straight to is_l / is_w instead of looping back to calc_first

        is_loss(bet, balance, *pbal);
        done = 0;
    }

    else if (dice == point) {
        is_win(bet, balance, *pbal);
        done = 0;
    }

    else {
        done = 0;

    }
    return done;
}




# include "header.h"

int main(void) {
    int mainbalance = 0;
    int choice = 0;
    int *pbal = &mainbalance;

    do {
        choice = print_menu();

        if (choice == 1) {
            mainbalance = get_balance();
            printf("Your balance is: %d\n", mainbalance);
            choice = 8; //reprints menu
            }

        if (choice == 3) {
            print_rules();
            choice = 8;
            }

        if (choice == 4)
            exit(1);

        if (choice == 2) {

            int bet = prompt_bet(mainbalance);
            int dice = roll_dice();
            int x = calc_first_roll(dice, bet, mainbalance, pbal);



            choice = 8;

            }

    } while (choice > 4 || choice < 1); //keeps running code until test becomes false.


    return 0;
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Ryan M
  • 23
  • 4
  • 1
    Your code is very incomplete. For example your parameter `balance` has no type, give him some! You are missing the header (though I suggest, that is only contains forward declaracations of the functions). Please fix your code and enable the compiler warnings, this should then solve it for you. – hellow Oct 08 '18 at 07:50
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – hellow Oct 08 '18 at 07:51
  • I can see quite a lot of warnings in your code, did you ignore compiler warnings, I'd suggest fix warning first – asio_guy Oct 08 '18 at 07:54
  • Always take the compiler's warnings serious. – alk Oct 08 '18 at 07:55
  • Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 08 '18 at 10:43
  • But I agree: this community is **not** meant like this: to just drop code that has many different issues, to then ask the community to review it to identify all issues and propose fixes for that. You best do as advised: fix all the warnings that the compiler gives you first. – GhostCat Oct 08 '18 at 10:44

1 Answers1

0

In this section:

if (dice == 7) {  //Goes straight to is_l / is_w instead of looping back to calc_first

    is_loss(bet, balance, *pbal);
    done = 0;
}

else if (dice == point) {
    is_win(bet, balance, *pbal);
    done = 0;
}

You're not passing is_loss and is_win the pointer, you're passing the integer value, that pbal points to. * outside of a declaration is always dereferencing.

So if calc_other_rolls gets int *pbal as an argument and you wanna pass it to another function that takes int * then you should do func(pbal) and not func(*pbal), since the second one passes the value not the pointer.

Edit: As @hollow pointed out, this gets flagged by the compiler if you enable warnings, so use them.

nullp0tr
  • 485
  • 3
  • 11