It's my first time using switch case statements in my code so I could just be using it wrong in this scenario. I am attempting to make a simple dice roller that lets you roll the dice again. My code for the dice roll works, but at the end when it asks you if you would like to roll again (y/n) When I enter y it just exits out.
`
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define WON 0
#define LOSE 1
int rollDice();
int playGame();
int rollAgain();
int rollDice()
{
return rand() % 6+ 1;
}
int playGame()
{
srand(time(NULL));
int dice_1 = 0;
int dice_2 = 0;
int sum = 0;
int result;
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("\n");
printf("Want to test your luck? ");
printf("Press ENTER to roll the die\n");
fgetc(stdin);
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
printf("Dice 1:%2d\nDice 2:%2d\nSum:%2d\n", dice_1, dice_2, sum);
switch ( sum )
{
case 7:
case 11:
result = WON;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 8:
case 9:
case 10:
case 12:
result = LOSE;
break;
}
return result;
}
int rollAgain()
{
srand(time(NULL));
int dice_1 = 0;
int dice_2 = 0;
int sum = 0;
int result;
printf("Press ENTER to roll the die\n");
fgetc(stdin);
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
printf("Dice 1:%2d\nDice 2:%2d\nSum:%2d\n", dice_1, dice_2, sum);
switch ( sum )
{
case 7:
case 11:
result = WON;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 8:
case 9:
case 10:
case 12:
result = LOSE;
break;
}
}
int main()
{
char answer;
int result = playGame();
switch ( result )
{
case WON:
printf("You won the game.\n");
printf("Do you wish to play again? (y/n)");
scanf("%c", &answer);
if(answer == 'y' || answer == 'Y');
{
int rollAgain();
}
break;
case LOSE:
printf("You lost the game.\n");
printf("Do you wish to play again?");
scanf("%c", &answer);
if(answer == 'y' || answer == 'Y');
{
int rollAgain();
}
break;
}
return 0;
}
`
How do I get my code to 'roll the dice again'? Why isn't the if statement working? is it because I am using a scanf instead of something like fgets? Like how I used the fgetc for the enter ?