0

First of all, I'm sorry if there is something obvious that I'm missing, I only recently started to learn how to code.

I'm trying to create a die for a board game but the while loop behaves weirdly and I couldn't figure out why. I would really appreciate it if someone could help me out.

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

int main() {
    int dice;
    char input;
    int game = 0;
    
    srand(time(0));
    
    while (game == 0) {
        printf("\nRoll the dice? (Y/N)\n");
        scanf("%c", &input);
        
        if (input == 'Y') {
            dice = (rand() % 6) + 1;
            
            printf("\ndice: %d\n", dice);
        }
    }

    return 0;
}

This is the output:

Roll the dice? (Y/N)                                                                                                                                   
Y                                                                                                                                                      
                                                                                                                                                       
dice: 2                                                                                                                                                
                                                                                                                                                       
Roll the dice? (Y/N)                                                                                                                                   
                                                                                                                                                       
Roll the dice? (Y/N)                                                                                                                                   
Y                                                                                                                                                      
                                                                                                                                                       
dice: 6                                                                                                                                                
                                                                                                                                                       
Roll the dice? (Y/N)                                                                                                                                   
                                                                                                                                                       
Roll the dice? (Y/N)
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • How many keys do you type when you enter a character? – William Pursell Jan 02 '21 at 14:14
  • 2
    Change `scanf("%c", &input);` to `scanf(" %c", &input);` with an added space. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Some explanation: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. – Weather Vane Jan 02 '21 at 14:15
  • If you're new to C, I strongly recommend you avoid `scanf` until you have a better grasp of the language. Do not waste your time learning the foibles and intricacies of the scanf format language. It might seem like a burden, but learning to use `fgetc` and `fread` and `fgets` and parsing the data yourself with `strtol`, etc. is a faster way to understanding the language. – William Pursell Jan 02 '21 at 14:16

1 Answers1

2

It is reading the new line character.

You can prove this by

scanf("%c", &input);
printf("Char read decimal value: %d\n", (int) input);

To fix, use

scanf(" %c", &input);

Please read the manual page. The leading space reads white space including new line.

PS: Please also check the return value from scanf. See scanf

Ed Heal
  • 59,252
  • 17
  • 87
  • 127