Solved

scanf-problem?

Posted on 2000-03-18
2
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

738 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