Solved

scanf-problem?

Posted on 2000-03-18
2
275 Views
Last Modified: 2010-04-15
Hi, could somebody please explain to me
why, when this program is executed it prints
>
then scans the char, prints the string corresponding to that alternative, then prints
>
one time, then once more
>
how come it does not stop on the first time?

File: linked.c

   node * head;
   char action;

   while(1){
      printf(" >");
      scanf("%c", &action);

      switch(action){
      case 'a' : printf("add");
                 break;
      case 'd' : printf("delete");
                 break;
      case 'q' : printf("quit");
                 exit(0);
      }
   }

}


0
Comment
Question by:sa9813
2 Comments
 
LVL 2

Accepted Solution

by:
mattrope earned 30 total points
ID: 2631482
When you type in something at the prompt and hit enter, two characters actually get stored in the STDIN buffer: the character you typed and a newline character.  The first character (the one you typed) gets stored in 'action' first, and then gets processed.  The next call to scanf finds something already in the buffer (a newline character) so it just uses that instead of waiting for something to be typed in.  Since you don't have a case for '\n' in your switch statement, your code doesn't do anything for the newline case except print out another prompt.

Hope that clears up your confusion.
0
 

Expert Comment

by:sylphe
ID: 2631487
Hello,

If my memory is not too bad :

When you do scanf, you ask for a char, but in input you will obtain a char and ENTER (which is necessary to know that the user entered something )  ENTER is also a char and this second char will remain in the input buffer. So to overcome this you should add :

fflush(stdin);

after your scanf. It will clean your stdin (input) stream
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now