0

I'm making a simple blackjack-like game as practice with the rand() func. In the for loop, every iteration of the print statement is repeated twice. However, I only want "Would you like to roll the die?" to be asked once in every iteration. Any idea as to what I'm doing wrong?

Code:

int main(void) 
{
    int x, y, z, roll, randint;
    char a,b;
    srand(time(NULL));

    printf("Hello Player! Welcome to BlackJack! This is a game of chance that provides fun for all!\n\n");

    printf("Rules:\nPlayers- Any number of players can participate, just make sure to specify the amount at the beginning.\nGoal- Be the closest to 21 points after 10 turns. No exceeding 21, or else you lose.\n\nEvery turn, the player has a chance to roll a die. However, they must choose whether or not they will roll. Once the die is rolled, they must keep the number of points that they gained.\n\nMost importantly, Have fun!\n\n");

    printf("Input the number of players: ");
    scanf("%d", &x);

    for(y=0; y<=5; y++)
    {
        for(z=0; z<x; z++)
        {
            printf("Would you like to roll the die?(y/n)");
            scanf("%c", &b);
            if(b=='y')
            {
                roll = rand() % 6+1;
                printf("You gained %d points.\n\n",roll);
            }
        }
    }
    printf("\n\nThank you for playing!");

    return 0;
}

Output:

Hello Player! Welcome to BlackJack! This is a game of chance that provides fun for all!

Rules:
Players- Any number of players can participate, just make sure to specify the amount at the beginning.
Goal- Be the closest to 21 points after 10 turns. No exceeding 21, or else you lose.

Every turn, the player has a chance to roll a die. However, they must choose whether or not they will roll. Once the die is rolled, they must keep the number of points that they gained.

Most importantly, Have fun!

Input the number of players: 2
Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 2 points.

Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 5 points.

Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 1 points.

Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 1 points.

Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 5 points.

Would you like to roll the die?(y/n)Would you like to roll the die?(y/n)y
You gained 5 points.



Thank you for playing! 
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
csmith
  • 1
  • https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer – Passerby Dec 08 '21 at 02:29
  • 1
    Add a space before `%c`, so `scanf(" %c", &b);`. This will skip leading whitespace, like the newline left in the buffer after the previous extraction. – Retired Ninja Dec 08 '21 at 02:30
  • For sanity, you almost always want to get input a line a a time (using `getline` - if your libc isn't compliant, it's easy to write or use a 3rd-party version), then parse that. Think about what happens if the user types `yy` or `yes` or `123`, etc. – o11c Dec 08 '21 at 02:34
  • Thanks! That fixed my problem! Could you explain why just adding that space before %c would fix it? I guess in my mind, the lack of a space wouldn't do much, but it clearly had a sizeable impact. I just don't really understand – csmith Dec 08 '21 at 15:03

0 Answers0