0

I've tried to use %c but then the printf afther while , prints 2 times. With %s I solved the problem but I get warnings. Also i tried a different version of the code with but after the printf the program stopped:

enter code here

char k;

while(isalpha(k)) 

printf("put number not a letter");

scanf("%d", &k);

********************************Code I have problem with is The one down there******************* **

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

int main(){

   unsigned char a;

    printf("Put a number");
    scanf("%c", &a);


    //that checks if the input contains letters 
    while(!(isdigit(a))){
        printf("you have put a letter not a number,put a number again:\n");
        scanf("%c", &a);

    }
    if(isdigit(a)){
        printf("you have put a number %c", a);
    } 
William Pursell
  • 204,365
  • 48
  • 270
  • 300
Leit22
  • 13
  • 3
  • The trouble is that when you type `a` and hit return, you enter two characters, neither of which is a digit. So, after reading and rejecting the `a`, it also reads and rejects the newline. You could help yourself (and your user too) by modifying the error message to something like: `printf("You entered %c, which is not a digit; please enter a digit: ");` (the newline is optional for a prompt). Use the [suggestion](https://stackoverflow.com/a/61152093/15168) by [@Breakpoint](https://stackoverflow.com/users/9345287/breakpoint) — use a leading blank in the format (but never a trailing blank). – Jonathan Leffler Apr 11 '20 at 04:29

1 Answers1

1
while(!(isdigit(a))) {
...
scanf(" %c", &a);
       ^ This is a white space

This will solve the problem of printing two times and warnings (if any)

Breakpoint
  • 428
  • 6
  • 19