Reading a character with scanf ()

This code is for craps .

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> int roll_dice(void); bool play_game(void); int main() { int i, ch,win = 0,lose = 0; bool flag; srand((unsigned)time(NULL)); do { flag = play_game(); if(flag) { printf("You win!"); win++; } else { printf("You lose!"); lose++; } printf("\n\nPlay again(Y/N)? "); scanf("%c", &ch); ch = getchar(); printf("\n"); }while(ch == 'Y' || ch == 'y'); printf("\nWins: %d Losses: %d",win,lose); return 0; } int roll_dice(void) { return rand()%6 + rand()%6 + 2; } bool play_game(void) { int sum = roll_dice(); printf("You rolled: %d\n", sum); if(sum == 7 || sum == 11) return 1; else if(sum == 2 || sum == 3 || sum == 12) return 0; else { int point = sum; printf("Your point is: %d\n", point); do { sum = roll_dice(); printf("You rolled: %d\n", sum); if(sum == 7) return 0; }while(point != sum); return 1; } } 

I only have a problem with a code snippet

  printf("\n\nPlay again(Y/N)? "); scanf("%c", &ch); ch = getchar(); printf("\n"); 

I used because it ends after one iteration with any user input Y or N I thought I was doing it wrong by placing ch = getchar() to eat \n , I deleted it and put a space in front of the conversion specifier and replaced it with " %c" , which also didn't work. When I replaced the conversion specifier with %d works fine.
Is there something wrong with this? I visited this post and it says the same thing as me.

+4
source share
4 answers

The marked code has undefined behavior, since ch is of type int , and the %c format specifier must match char .

When I replaced the conversion specifier% d, it works fine.

When you switch to %d , scanf() crashes because Y or Y not int , so no input is consumed (except for the leading space, which discards the new line character during subsequent iterations of the loop), and the subsequent ch = getchar() actually reads the character entered by the user, and the code works with fluke. Always check the return value of scanf() , which returns the number of jobs completed.

+4
source
 scanf("%c", &ch); ch = getchar(); 

And how did you lose the previous char stored in ch . What about

 ch = fgetc(stdin); while (fgetc(stdin) != '\n') ; 

instead

+3
source

You convert a character with scanf () and then overwrite it with getchar () immediately afterwards. I would not expect it to work if you did not type "yy" before entering ENTER, but then your second confirmation will fail.

BTW, use the space in " %c" .

+3
source
 printf("Play again? "); scanf(" %c", &char); 

this code works for me. The project is made from the book of K.N. King "With programming: a modern approach." I have encountered this problem before and had the same problem. On page 224 there is an example guess.c project that includes the exact same โ€œaskโ€ (โ€œplay againโ€) command. And the author used scanf ("% c", & command); (he used the command instead of ch) and it worked. I remember using it during the dice project, but that didn't work. I guess I missed something. In general, an expression above 100% works.

+2
source

Source: https://habr.com/ru/post/1489986/


All Articles