Solved

fflush

Posted on 2006-11-22
7
834 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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 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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

809 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