Hi I am implementing a function that check if user is a man or woman.
But it does not worl properly.

I press e for example and appears two times
Please press m/M for Man
w/W for

Also sometimes w/W/m/M are not recognised and appears the above.

My cose is :

char ask_if_man_or_woman()
{
     char choice_char;
     
     printf("Please answer me (w/m)\n");
     scanf("%c",&choice_char);   
 
     while((choice_char!='w')&&(choice_char!='W')&&(choice_char!='m')&&(choice_char!='M'))
     {
            printf("\n\t Please press m/M for Man\n");
            printf("\t              w/W for Woman\n\n");    
            
            scanf("%c",&choice_char);   
         
            printf("\n");
     }
                   
return choice_char;      
}

Any ideas please ????

Dani AI

Generated

Quick diagnosis and root cause
The behaviour you saw (extra prompts, valid letters sometimes “not recognised”) is almost always an input-buffer issue: reading a single character with scanf("%c", ...) consumes the typed character but leaves the newline from the Enter key in stdin. The next read immediately takes that newline instead of waiting for new input. ’s attempt used a different read method but their conditional was logically incorrect (the ('w' || 'W' || ...) pattern does not test multiple characters). reported that fflush(stdin) appeared to fix it — that may work on some compilers but is not portable.

Why not use fflush(stdin)
fflush on input streams is undefined by the C standard; it is only reliably defined for output/update streams. Relying on it will make the program non-portable. See the reference for details: fflush - cppreference.

A robust, portable pattern
Read a whole line and then examine the first non-space character. This avoids leftover-newline problems and makes validation straightforward. Use fgets for line input, skip leading whitespace, normalize with tolower (casting to unsigned char first), and accept m or w. Example pattern:

char buf[16];
if (fgets(buf, sizeof buf, stdin)) {
    char *p = buf;
    while (*p && isspace((unsigned char)*p)) p++;
    if (*p) {
        char c = tolower((unsigned char)*p);
        /* check c == 'm' or c == 'w' */
    }
}

Practical tips

  • If you prefer scanf, use scanf(" %c", &ch) (note the leading space) to skip whitespace.
  • When using getchar(), keep the result in an int so you can detect EOF.
  • Always cast to unsigned char before passing to ctype.h functions.
  • Avoid fflush(stdin) for portability; if you must discard input, read and discard until '\n' or EOF.

Recommended Answers

All 3 Replies

Please read this for help on

Also check one of our posts on DaniWeb, keypress detection

char ask_if_man_or_woman()
{
  int choice_char;

  do
  {
printf("\n\t Please press m/M for Man\n");
printf("\t w/W for Woman\n\n");
choice_char=getchar();


  }while(choice_char==('w' || 'W' || 'm' || 'M'));
  return choice_char;
}
char ask_if_man_or_woman()
{
      int choice_char;

      do
      {
    printf("\n\t Please press m/M for Man\n");
    printf("\t w/W for Woman\n\n");
    choice_char=getchar();


      }while(choice_char==('w' || 'W' || 'm' || 'M'));
      return choice_char;
}

Thanks a lot
The problem solved using fflush(stdin)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.