LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fflush fuction in c (https://www.linuxquestions.org/questions/programming-9/fflush-fuction-in-c-39348/)

jetfreggel 12-25-2002 09:56 AM

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)

boku 12-25-2002 10:32 AM

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.

jetfreggel 12-25-2002 10:40 AM

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

boku 12-25-2002 11:58 AM

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' );

jetfreggel 12-25-2002 01:07 PM

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

purpleburple 12-25-2002 09:37 PM

Im new to C too, but it seems as though fflush only flushes up until 'new space'. I may be wrong.

GtkUser 12-26-2002 02:57 AM

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;


jetfreggel 12-26-2002 05:04 AM

thx for all the replies but i still don't know why fflush()

is it ANSI-standard????

GtkUser 12-26-2002 06:25 AM

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.

Malicious 12-26-2002 06:52 AM

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.

Malicious 12-26-2002 07:20 AM

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.

jetfreggel 12-26-2002 07:52 AM

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);
}

GtkUser 12-26-2002 05:24 PM

gets(junk)

is unsafe, use

fgets(junk,255,stdin);

jetfreggel 12-26-2002 06:02 PM

thx again for the advice


All times are GMT -5. The time now is 07:27 AM.