Why is fflush needed here?

This is a discussion on Why is fflush needed here? within the C Programming forums, part of the General Programming Boards category; OK, this is a part of a program that I wrote: Code: #include <stdio.h> #include <string.h> struct info { char ...

  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,006
    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,796
    >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, 10: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, 12:55 PM
  3. Binary search needed
    By ItsMeHere in forum C Programming
    Replies: 1
    Last Post: 06-22-2006, 08:26 AM
  4. To fflush, or Not to fflush...
    By bjgough in forum C Programming
    Replies: 32
    Last Post: 08-19-2005, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21