Solved

fflush

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

Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 97
reading tzdatabase for timezone definitions 5 128
C hashtable library 3 81
Programming Arduino to control a Max7219 using C 2 113
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 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 nested-loops in the C programming language.

929 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