Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

c program not giving proper output. Kindly help.

+3 votes
asked May 18 by Vikas Arya (200 points)
#include<stdio.h>

int main()

{

char a;

int choice=1;

  while(choice!=2)

  {

printf("\nenter choice\t1.Insert\t2.Exit\t");

    scanf("%d",&choice);

    if (choice==1)

  {

printf("enter character ");

  scanf("%c",&a);

  printf("Entered character is : %c",a);

}

  }

  

return 0;

}

2 Answers

+1 vote
answered May 18 by Devang Patel (460 points)
selected May 19 by Vikas Arya
 
Best answer
When you insert '1' and then press enter key. The variable 'a' will store the newline character ('\n') which is why it prints a newline. Just insert getchar(); after 1st scanf(); to fix the problem.
commented May 19 by Vikas Arya (200 points)
thanks a lot. It worked.
Still i'm finding it a bit difficult to understand.
Whenever we use scanf to take a character input, do we have to check the last input? And does it apply to other kinds of inputs also, like integer? And what function does getchar() do here. It reads the previous character or the one user will type next. I thought that getchar takes the next input(also thought the same for scanf)
Thanks in advance.
commented May 19 by Devang Patel (460 points)
edited May 19 by Devang Patel
When you press enter, you are passing two inputs; '1' and '\n' (Newline character caused by Enter Key). The 1st scanf(); accepts only integer type so '1' is accepted because its is a valid integer, Next in line is the '\n' which is not an integer data type so now the job of 1st scanf(); is completed which was to accept an integer. The 2nd scanf(); is waiting for a char data type and since the newline character ('\n') is a well.. char data type, it gets accepted.

By using getchar(); after 1st scanf(); you are telling your program that after you accept an integer, I want you to accept an extra character(probably newline character caused by enter key) so that the 2nd scanf(); can wait for another character which the users expects to type.

The original program would run just as expected if you change the data type of variable 'a' to int data type but that would defeat the purpose of the program which was to accept characters instead of integers.

scanf("%d", variable); -> only accepts input of type %d.
scanf("%f", variable); -> only accepts input of type %f.
scanf("%c", variable); -> only accepts input of type %c.
commented May 19 by Vikas Arya (200 points)
Thank you so much for the explanation.  Now i am able to understand how it is working. I was stuck on this problem for a whole day before i posted the problem. Thanks a lot for you time.
+1 vote
answered May 18 by Peter Minarik (58,190 points)
edited May 19 by Peter Minarik

When you try to read the next character, the whitespace characters need to be ignored, otherwise, you'd just read the ENTER from your last input (number 1).

To tell scanf() to ignore the whitespace, add a space before the %c

scanf(" %c", &a);
commented May 19 by Vikas Arya (200 points)
Thank you so much. It worked.
 Still i'm finding it a bit difficult to understand.
Whenever we use scanf to take a character input, do we have to check the last input? And does it apply to other kinds of inputs also, like integer? And how does the 'space' before the  %c ignores the previous input(the ENTER in this case). I thought everything between quotes( " ") is ignored except for placeholders like %c or %d etc.
Thanks in advance.
commented May 19 by Peter Minarik (58,190 points)
I've modified my answer a bit, only to add a link to the documentation of scanf(). Check it out, there are plenty of details on how scanf() works.

To answer your question: see the manual (linked in my answer) that says:

--- QUOTE BEGINS ---
Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
--- QUOTE ENDS ---

That is to say, scanf() recognises the space character before in the format string and it automatically ignores any whitespace characters left over from other previous user inputs. No need to manually try to call further functions to consume leftover characters.

Normally, this is only a problem with reading characters, so if you want to read numbers, you shouldn't face the same issue. However, stating that you want to ignore white spaces could always be an option.

Also, it is bad practice to mix fgets() (https://www.cplusplus.com/reference/cstdio/fgets/) and scanf(), as they have different idea about the consumption of the whitespace characters and you'd face similar issues as you had before. If you'd ever want to use fgets(), keep using that and don't mix it with scanf() (or use scanf() and dont' use fgets() at all).

I hope this helps.
commented May 19 by Vikas Arya (200 points)
it did help a lot. I went through the link that you included in your comment. It was of great help. Thank you for your time. It was very kind of you.
commented May 20 by Peter Minarik (58,190 points)
My pleasure.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...