Solved

fflush

Posted on 2006-11-22
7
845 Views
Last Modified: 2011-10-03
in my code there is a part like

scanf("%c",&sex);
fflush(stdin);      
printf("Enter the choice (C/W)");
scanf("%c",&choice);
fflush(stdin);      


it works on windows; but on fedora5 it doesnot clear the cache, what can i do for this?
0
Comment
Question by:btocakci
[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
7 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17999178
Hi btocakci,

This is a common problem when porting. The original coder made use of 'undefined' behaviour.

I dont know a concrete, simple route around this, there are many alternatives. Essentially you need to consume without blocking, all data until a '\n' is read. If you can trust your users not to attempt buffer overflow hacking, this method is the most trivial:

char trash[256];

...

scanf("%c%s",&sex,trash);
printf("Enter the choice (C/W)");
scanf("%c%s",&choice, trash);

It is certainly not my favourite but it can be done with a fairly simple search/replace.

A second approach could be something like:

#define flusstdin while ( ... ) {<Insert your consumer here.>}

Again, a simple search/replace can fix all the abuse then.

Paul
0
 
LVL 84

Expert Comment

by:ozo
ID: 17999562
fflush is only defined to work on output streams
some systems also define a non-standard fpurge that works on input streams
0
 
LVL 24

Accepted Solution

by:
fridom earned 63 total points
ID: 18001159
The better answer wouild be
use a combination of fgets and sscanf


char buf[SOME_LIMIT]
char * pc = fgets(buf, sizeof(buf), stdin)
if (! pc)
 // error handling
...
int vals_read
int val;
vals_read = sscanf(buf, "%d", &val);
if (vals_read != 1)
  // error handling


Regards
Friedrich
0
 

Expert Comment

by:karana
ID: 18006410

please scanf from new line


scanf("%c",&sex);

printf("Enter the choice (C/W)");
printf("\n");
scanf("%c",&choice);

     
0
 
LVL 2

Assisted Solution

by:Razor2k5
Razor2k5 earned 62 total points
ID: 18015064
Hi,

in order to have a more riguros approach use formatted read:

while (scanf("%d.%d.%d", &x, &y, &z) != 3) {
     while (getchar() != '\n'); /* line consuming
     printf("Try again...\n");
}
0

Featured Post

Industry Leaders: 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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

688 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