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 ????

Recommended Answers

All 3 Replies

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.