Home > database >  Can't get my code to continue when I enter 'y', it just exits out
Can't get my code to continue when I enter 'y', it just exits out

Time:12-01

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:-\nDice 2:-\nSum:-\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:-\nDice 2:-\nSum:-\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 ?

CodePudding user response:

The following code contains two errors:

if(answer == 'y' || answer == 'Y');
{
    int rollAgain();  
}

The line

if(answer == 'y' || answer == 'Y');

is wrong. The ; at the end of the line is an empty statement, so you are effectively telling the program to "do nothing" when the if condition is true. If you instead want the code block after the if statement to be associated with the if statement, you must remove the empty statement, by removing the ; in the line quoted above.

My gcc compiler warns me of this, when I enable all warnings. You may want to read this question: Why should I always enable compiler warnings?

Also, the line

int rollAgain();  

is wrong. That line is a function declaration. If you want it to be a function call instead, you must remove the int from that line.

After fixing these two bugs, which both occur in two places in your program, the function rollAgain is now successfully being called:

--------------
- HOW TO WIN -
--------------
Your dice roll must equal 7 or 11 or else you lose.

Want to test your luck? Press ENTER to roll the die

Dice 1: 4
Dice 2: 6
Sum:10
You lost the game.
Do you wish to play again?y
Press ENTER to roll the die
Dice 1: 6
Dice 2: 5
Sum:11

However, it does not make much sense that you only allow the user to repeat once. It also does not make sense that you inform the user whether they won the first round, but not whether they won the second round.

It also does not make much sense that you have two functions playGame and rollAgain that are nearly identical. The main difference between these functions seems to be that the first function prints additional instructions to the user, which are only intended to be printed before the first round. Therefore, it would make sense to combine these two functions into one function, so that you don't have any unnecessary code duplication. The printing of the additional instructions could be handled in another function, which is called in main at the start of the program.

Another issue is that the line

scanf("%c", &answer);

in the function main will consume the y character from the input stream, but leave the newline character on the input stream. This means that when the function rollAgain is called, it will prompt the user like this:

Press ENTER to roll the die

However, afterwards, the line

fgetc(stdin);

will not wait for the user to press ENTER, but will instead immediately read the \n newline character that was left over by the function main.

In order to prevent such leftover characters, I recommend that you always read exactly one line of input at once, including the newline character, for example by using the function fgets instead of scanf or fgetc.

Another issue in your program is that you are calling srand several times in your program, which will cause the same sequence of random numbers to be generated if srand is called in the same second. See the following question for further information: srand() — why call it only once?

Here is a fixed version of your code, in which all the issues mentioned above have been addressed and some minor improvements have been added:

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

enum result
{
    RESULT_WON = 0,
    RESULT_LOST = 1
};

int rollDice();
enum result playGame();

int rollDice( void )
{
    return rand() % 6   1;
}

void printInstructions( void )
{
    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? ");
}

enum result playGame( void )
{
    int dice_1;
    int dice_2;
    int sum;
    int c;

    printf("Press ENTER to roll the die\n");

    //read characters from input stream until newline character
    //is found
    do
    {
        c = getchar();

    } while ( c != EOF && c != '\n' );

    dice_1 = rollDice();
    dice_2 = rollDice();

    sum = dice_1   dice_2;

    printf( "Dice 1:-\nDice 2:-\nSum:-\n", dice_1, dice_2, sum );

    switch ( sum )
    {
        case 7:
        case 11:
            return RESULT_WON;
        default:
            return RESULT_LOST;
    }
}

int main( void )
{
    char line[200];

    //seed the random number generator only once, at the
    //start of the program
    srand(time(NULL));
  
    printInstructions();

    do
    {
        switch ( playGame() )
        {
            case RESULT_WON:
                printf( "You won the game.\n" );
                break;
            case RESULT_LOST:
                printf("You lost the game.\n");
                break;
        }

        printf( "Do you wish to play again? (y/n)" );

        //read exactly one line of input
        if ( fgets( line, sizeof line, stdin ) == NULL )
        {
            fprintf( stderr, "Input error!\n" );
            exit( EXIT_FAILURE );
        }

    } while ( toupper( (unsigned char)line[0] ) == 'Y' );

    return EXIT_SUCCESS;
}

This program has the following behavior:

--------------
- HOW TO WIN -
--------------
Your dice roll must equal 7 or 11 or else you lose.

Want to test your luck? Press ENTER to roll the die

Dice 1: 3
Dice 2: 6
Sum: 9
You lost the game.
Do you wish to play again? (y/n)y
Press ENTER to roll the die

Dice 1: 4
Dice 2: 1
Sum: 5
You lost the game.
Do you wish to play again? (y/n)y
Press ENTER to roll the die

Dice 1: 3
Dice 2: 4
Sum: 7
You won the game.
Do you wish to play again? (y/n)n
  • Related