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;
}
}
}