-1

I am not able to give input every time. It is skipping in between and I can only give input at alternate times I have created a do while loop and trying to take input as char.

#include <stdio.h>

int main()
{
   char gender;
   do{
       printf("Enter gender\n");
       scanf("%c",&gender);
       if(gender=='m')
       printf("Male\n");
       else
       printf("Other\n");
   }while(1);
}

Gaurav R
  • 9
  • 3
  • 2
    Please do not use images of code but put your code directly in the question. – Simon Doppler Mar 09 '22 at 07:02
  • Please provide a textual [mre] directly here. Not linked. Not picture of code. ( https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 ) Without, this lacks debugging details. – Yunnosch Mar 09 '22 at 07:02
  • 1
    Paste your code here,So we can figure out easily – Arun Mar 09 '22 at 07:11
  • @Yunnosch I am sorry , I am really new to this platform and coding. I am figuring all these stuff now. I'll definitely take this into consideration next time. Thank you. – Gaurav R Mar 09 '22 at 08:29
  • Please take it into account this time already. Keep in mind that the idea of StackOverflow is to make a Q/A collection for everybody. Surprisingly not to help individuals. Your question as it is does not match StackOverflow goals. Please improve it or delete it. – Yunnosch Mar 09 '22 at 08:31
  • Duplicate: [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Lundin Mar 11 '22 at 09:13

1 Answers1

-1

The scanf() read the new line too as a character and assign it to next char value.
Thats why you can able to read alternatively.
So read a dummy character next to it solve this.
Try this and let me know

#include <stdio.h>

int main()
{
   char gender,temp;
   do{
       printf("Enter gender\n");
       scanf("%c%c",&gender,&temp);
       if(gender=='m')
       printf("Male\n");
       else
       printf("Other\n");
   }while(1);
}
Arun
  • 67
  • 11
  • 2
    Please don't encourage "pictures of code" questions by answering them. Also this is a super common FAQ so we could as well close the question as a duplicate instead of answering. – Lundin Mar 09 '22 at 07:31
  • 1
    Sorry for that sir!. He is a new contributor that why i answered. – Arun Mar 09 '22 at 07:36
  • Please do not teach new contributors that they do not need to follow the rules. It does not help StackOverflows goals and it will sooner or later frustrate the new users. Instead teach them how to improve. Then answer to make a Q/A which helps others AND the OP. – Yunnosch Mar 09 '22 at 08:32
  • @Gaurav R accept the answer and close the question – Arun Mar 11 '22 at 10:07