Thread: Why is fflush needed here?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    Why is fflush needed here?

    OK, this is a part of a program that I wrote:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct info {
    
    	char firstname[20+1];
    	char lastname[25+1];
    	float score;
    };
    
    int main() {
    
    	struct info student;
    
    	do {
    
    		printf("Type in the name: ");
    		gets(student.firstname);
    
    		if (strlen(student.firstname)==0) break;
    
    		printf("Type in the last name: ");
    		gets(student.lastname);
    
    		printf("Type in the score: ");
    		scanf("%f", &student.score);
    		//fflush(stdin);	
    		
    
    	} while (1);
    
    	return 0;
    }
    It didn't work this way (it allows me to enter data only once), so I put fflush(stdin) for no particular reason and now it works. Why is that?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Last edited by Dave_Sinkula; 11-11-2004 at 05:19 PM. Reason: Added more links.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why is that?
    You're lucky.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Binary search needed
    By ItsMeHere in forum C Programming
    Replies: 1
    Last Post: 06-22-2006, 07:26 AM
  4. To fflush, or Not to fflush...
    By bjgough in forum C Programming
    Replies: 32
    Last Post: 08-19-2005, 12:56 AM
  5. When and where to use fflush (stdout)?
    By Micko in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 11:58 AM