I have an assignment to make a craps game in c. The program i wrote runs but it always ends before it should. I think that the while loops in main have a problem but im not exactly sure wat the problem is because ive reviewed this code over and over and it seems logical but somehow its not working properly. The loop is supposed to call the play_game function over and over again as long as the input from the user is 'Y' or 'y'. when you run the program the loop is calls the play_game function, outputs an answer and then it asks the user if they want to play again but instead of waiting for the input it just ends. I can't figure out why. Would really appreciate some help. Thanks.

#include<stdio.h>
#include <stdlib.h>
#include <time.h>

int roll_dice(void);
int play_game(void);

int main()
{
    int wins = 0, loss = 0, result;
    char answer;

    result = play_game();

    while(result == 1)
    {
        printf("You win!\n\n");
        wins++;
        printf("Play Again? ");
        scanf("%c", &answer);

        if(answer == 'Y' || answer == 'y')
        {
            result = play_game();
        }
        else if(answer != 'Y' || answer != 'y')
        {
            break;
        }
    }

    while(result == 0)
    {
        printf("You lose!\n\n");
        loss++;
        printf("Play Again? ");
        scanf("%c", &answer);

        if(answer == 'Y' || answer == 'y')
        {
            result = play_game();
        }
        else if(answer != 'Y' || answer != 'y')
        {
            break;
        }
    }

    printf("\nWins: %d  Losses: %d", wins, loss);

    return 0;
}

int roll_dice(void)
{
    const int LOW = 1;
    const int HIGH = 6;
    int first_die, sec_die, total;

    srand((unsigned) time(NULL));

    first_die = rand() % (HIGH - LOW + 1) + LOW;
    sec_die = rand() % (HIGH - LOW + 1) + LOW;

    total = first_die + sec_die;

    return total;
}

int play_game(void)
{
    int check, point;

    check = roll_dice();

    if (check == 7 || check == 11 )
    {
        printf("You rolled: %d\n", check);
        return 1; //1 = true and 0 = false
    }
    else if(check == 2 || check == 3 || check == 12)
    {
        printf("You rolled: %d\n", check);
        return 0;
    }
    else
    {
        point = check;

        printf("You rolled: %d\n", check);
        printf("Your point is: %d\n", point);

        check = 0;
        check = roll_dice();

        while(check != point && check != 7)
        {
            printf("You rolled: %d\n", check);
            check = roll_dice();
        }

        if(check == point)
        {
            printf("You rolled: %d\n", check);
            return 1;
        }

        if(check == 7)
        {
            printf("You rolled: %d\n", check);
            return 0;
        }

    }

}

It's because when you enter Y or y aren't you also pressing [enter]? You are leaving the [enter] in the buffer for the next input statement. You need to clear your buffer. I leave that exercise for you to search out.

A couple problems, though:
Never call srand() inside your program. Call it only once at the beginning of main() . You are seeding the random generator, not growing a forest of generators :icon_wink:

If you check if 'y' or 'Y' is entered, what are the possible values for the else condition? Is there really a need to test if the value is not 'y' or 'Y'?

Why are you calling play_game() then you have 2 WHILE loops to catch the result? Call the play_game() function only inside one single WHILE loop and use your YES choice to exit that loop. Your program will be much smaller and easier to follow. And you won't need all that IF nonsense at the end of the loop since the WHILE acts as an IF anyway.

Thanks! I'll be sure to try what you suggested!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.