# Getting a double puts() output while working on the dice game

 Chris Pilcher · August 11, 2015 So, I decided to finally get around to learning C, since Python and C# can only do so much for me.While watching your videos, I got to the part where we're supposed to make a dice game. So I made a dice game!All the game logic works just fine. My one problem is that I keep getting a double puts() output at one point in my code.Now, there is definitely only one puts() call in my code (which I'll stick at the end). Also, there is no ostensible way for the loop that the statement is in to iterate before accepting input.It's kind of a small issue, but I really want to figure out what's going on. I tried replacing it with a printf statement, but it still printed twice, so I figure it has to be something wrong with my while loop.Here is my code (I added a comment in here, and the problem is on line 60:``#include #include /* * Todo: * Cleanup * Make "checkWin" function *//** Function Prototypes */int diceRoll(int sides);/** Entry point * * Game starts here */int main(){    int sides = 0;    int sum, roll, average, i;    int win;    char inputBuffer [100];    char input = 'z';    while (1)    {        sum = 0;        input = 'z';        puts("How many sides do your dice have? \nEnter a number less than two if you want to quit");        // Get user input        scanf(" %d", &sides);        printf("\n");        // Check if the player entered a number less than two.        if(sides < 2)        {            puts("Hokay, see ya later!");            break;        }        puts("Okay, rolling three dice:\n");        // Roll three dice        for(i = 0; i < 3; i++)        {            printf("Roll %d: ", i + 1);            roll = diceRoll(sides);            sum += roll;        }        average = sum / 3;        printf("Your average roll was %d\n\n", average);        // Check if the user thinks it will be higher, lower, or the same        // DO this by taking the first character of their input and changing it to a lowercase letter        while(tolower(input) != 'h' && tolower(input) != 'l' && tolower(input) != 's')        {            // THIS IS WHERE THE ERROR IS OCCURING!            puts("Do you think the next roll will be higher, lower or the same?");            gets(inputBuffer);            input = inputBuffer[0];        }        // Roll one more die.        printf("Last roll: ");        roll = diceRoll(sides);        //Todo: Roll this into a function that returns win or lose.        // If the roll was higher than the previous average. . .        if(roll > average)        {            // . . .and the user was wrong            if(input != 'h')            {                win = 0;            }            else            {                win = 1;            }        }// Else if the roll is less than the previous average. . .        else if(roll < average)        {            // . . .and the user was wrong            if(input != 'l')            {                win = 0;            }            else            {                win = 1;            }        }// If roll was the same as the average. . .        else        {            // . . .and the user was wrong            if(input != 's')            {                win = 0;            }            else            {                win = 1;            }        }        // Check if user won        if(win)        {            puts("Congratulations! You won!\n\n");        }        else        {            puts("Wow, you kinda suck at this, huh?\n\n");        }    }    return 0;}// Takes input sides and then simulates a single dice roll//Todo: Make percentage die possibleint diceRoll(int sides){    int roll = (rand() % sides) + 1;    printf("%d\n\n", roll);    return roll;}``

## Replies

 c student · August 12, 2015 ``scanf(" %d", &sides);``your scanf will read a number however when you press enter to submit it to stdin, the \n will remain inside stdin.  when you call gets the first time, it will read the \n without you realizing.  use the following to suppress the newline:``scanf(" %d%*c", &sides);``also, don't use gets.  use fgets.scanf manpage: http://linux.die.net/man/3/scanffgets manpage: http://linux.die.net/man/3/fgets
• 1

## C

130,869 followers

One of the most popular languages of all time.