Solved

scanf-problem?

Posted on 2000-03-18
2
288 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 137
How do I avoid pointer to integer casting errors in C programming? 4 208
negation in C function 14 144
Problem to save 10 150
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

896 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