Thread: fflush(stdin)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69

    fflush(stdin)

    okay. I know this function is wrong, but I have an example where the code doesn't exectute as it should when I leave it out. I'm afraid the code is commented in german, sa are the variables.

    Basically what it does is read in 3 sets of two strings and a number into a struct, and then write the values to a file.

    Now after reading the values to the struct there is an fflush(stdin). If I leave it out, on the second(/3) time writing to the struct, it only asks fo rthe second string.

    I'm using gets to read the strings into the struct and fprintf to write to the file. Any ideas?..

    I won't add all the code cos I'm pretty sure the problem lies in the following segment, however if somebody want's to see the problem themselves, just say and I'll put up the whole program. It's relatively short anyway...

    Code:
    int readin()
      {
         printf("Name: ");       
         gets(aktuell->name);
         printf("Vorname: ");
         gets(aktuell->vorname);
         printf("PLZ: ");
         scanf("%d",&aktuell->PLZ);
    
         fflush(stdin);
    
    fprintf(fp, "%20s %20s %5d", aktuell->name, aktuell->vorname, aktuell->PLZ);
    WITH the fflush(stdin) the Program reads:

    Code:
    Name: (I enter xxx)
    Vorname: xxx
    PLZ: (I enter yyy cos it's a number)
    
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    AND WITHOUT:

    Code:
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    
    Name: Vorname: xxx
    PLZ: yyy
    
    Name: Vorname: xxx
    PLZ: yyy
    the output on the first is right, the second returns nothing on the last two Name's...

    Many thanks in advance,..

    ~mako

    €DIT: Please don't link me to the FAQ, I've read th epart on fflush(stdin) and it doesn't answer why it works here and doesn't without...

    And I don't think it's compiler related here either. If you don't believe me, ask for the full program and try executing yourself... Thx again...
    Last edited by mako; 01-20-2006 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM