LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-25-2002, 09:56 AM   #1
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Rep: Reputation: 30
fflush fuction in c


hi everyone
i am trying to learn c but i 've encountered something
i am learning from a book (so i can't ask a teacher)
i use gcc and when i try to use fflush for removing junk characters
the code says
#include <stdio.h>


int main(void)
{
int age;
char name[20];

puts("Enter your age: ");
scanf("%d", &age);

fflush(stdin);


puts("Enter your first name: ");
scanf("%s", name);

printf("Your age is %d\n", age);
printf("Your first name is %s\n", name);

return 0;
}

but it doesn't work

could somebody explain me why(simple i am newbie on this subject and a lot other subjects i think)



and another question is there a site or could someone supply me with exercises
and as last i've seen an intro to pointers but i don't see so much the advantage of it (maybe when i get to advanced use of it)
 
Old 12-25-2002, 10:32 AM   #2
boku
Member
 
Registered: Aug 2002
Location: Sweden
Distribution: Gentoo
Posts: 43

Rep: Reputation: 15
I could not find any error. What is it that isn't working?

Read here about fflush: http://www.cplusplus.com/ref/cstdio/fflush.html

The best way to learn is, I think, to experiment on your own. Try to accomplish something and see it work.

You are already using pointers! The second parameter to scanf is a pointer.
Pointers are very useful, and not that complicated.
 
Old 12-25-2002, 10:40 AM   #3
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
at the recommended site i compiled it and got this as answer
Enter some words: testing this program
The first word you entered is : testing
Enter some words: The first word you entered is : this

and at mine code
Enter your age:
26 and counting
Enter your first name:
Your age is 26
Your first name is and

could there be something wrong with gcc
 
Old 12-25-2002, 11:58 AM   #4
boku
Member
 
Registered: Aug 2002
Location: Sweden
Distribution: Gentoo
Posts: 43

Rep: Reputation: 15
hmm.. I don't know. Perhaps flushing stdin isn't defined or standardized. Maybe someone else knows??

In this particular case you can probably get away with this, though. Replace:

fflush(stdin);

with:

while( getchar() != '\n' );
 
Old 12-25-2002, 01:07 PM   #5
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
i replaced it with

void clear_kb(void)
{
char junk[255];
gets(junk);
}
but i want to know why it doesn't work on my system

gcc-3.2
 
Old 12-25-2002, 09:37 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Im new to C too, but it seems as though fflush only flushes up until 'new space'. I may be wrong.
 
Old 12-26-2002, 02:57 AM   #7
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Re: fflush fuction in c

Code:
#include<stdio.h>


int main(void)
{
	int age;
	char name[20];

	printf("Enter your age: ");
	scanf("%d", &age);

	// fflush(stdin); //undefined!!!!!
	while (getchar() != '\n') continue;

	printf("Enter your first name: ");
	scanf("%s", name);
	while (getchar() != '\n') continue;

	printf("Your age is %d\n", age);
	printf("Your first name is %s\n", name);

	return 0;
 
Old 12-26-2002, 05:04 AM   #8
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx for all the replies but i still don't know why fflush()

is it ANSI-standard????
 
Old 12-26-2002, 06:25 AM   #9
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
It's in the standard but it should be used for flushing standard output:

fflush(stdout);

However flushing standard input is undefined as stated in the standard:

fflush(stdin);

The reason why you need to clear the standard input buffer after you use scanf is due to the fact that scanf leaves the newline character in the standard input buffer. You need to remove the newline or else it will screw up subsequent scanf calls. On the other hand if you used a function like fgets than the newline is not left in the standard input buffer so there is no problem. The fix for scanf is to use: while (getchar != '\n'); to flush the buffer because fflush(stdin) is undefined! It can crash your program or cause some undefined result.

Last edited by GtkUser; 12-26-2002 at 06:26 AM.
 
Old 12-26-2002, 06:52 AM   #10
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Both ANSI and POSIX state that the result of an "fflush()" call on a stream where the last action was a "read()" is undefined. The link you posted above may be true for some C compilers, but in general is bum dope. Both gcc and g++ and the generated objects work correctly.
 
Old 12-26-2002, 07:20 AM   #11
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Just an additional comment; fflush() is for ANSI C (FILE *) streams. C++ streams have a similar ::flush manipulator that is only defined for output (ostream) streams. C++ also has an ::ignore manipulator for input (istream) streams which does sort of what you expect fflush(stdin) to do.
 
Old 12-26-2002, 07:52 AM   #12
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
OK , thx for tour replies
i will leave it for the stdin as it is and try the advice you gave
yes the book stated that the scanf leaves the rest of the keys
in the keyboard buffer and the next you accessed the input from the keyboard(will get unexpected results)

but now i'm puzzled why they mentioned it in the book

ah well i'll leave it like it is

has someone any ideas for exercises for a newbie in c???


thx again for your replies

p.s. couldn't i also use the next function or are there any

disadvantages

void clear_kb(void)
{
char junk[255];
gets(junk);
}
 
Old 12-26-2002, 05:24 PM   #13
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
gets(junk)

is unsafe, use

fgets(junk,255,stdin);
 
Old 12-26-2002, 06:02 PM   #14
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx again for the advice
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fflush and fsync? power123 Programming 6 11-02-2005 11:04 PM
gets fuction gives warning, why? sharapchi Slackware 4 11-01-2005 10:31 AM
C code fflush/fpurge/getchar problems boxerboy Programming 2 08-29-2005 01:46 PM
How to make a winmodem fuction on Mandrake 9.2 Impaler Linux - Hardware 1 04-02-2004 06:23 AM
need perl fuction like c #include statement for header file mrtwice Programming 5 06-19-2003 02:42 PM


All times are GMT -5. The time now is 05:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration