Thread: Problems with fgets not accepting input from stdin

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    Problems with fgets not accepting input from stdin

    Greetings,

    I've been tasked to create a menu driven program that accepts various inputs from the user and displays the data back to them. Relatively simple I'm sure but I'm having a problem with fgets not pausing for a string entry.

    Here is the code I've written so far:

    Code:
    void perform_menu_tasks( int e ) {
    	
    	if( e == 1 ) {
    		printf("Enter the opponent's name: ");
    		fgets( user_input.opp_name, sizeof (user_input.opp_name), stdin );
    		
                         // Code will be entered here to remove newline character.
    	}
    	else if( e == 2 ) {
    		printf("Enter USC's points: ");
    		scanf("%d", &user_input.usc_points);
    		}
    	else if( e == 3) {
    		printf("Enter opponent's points: ");
    		scanf("%d", &user_input.opp_points);
    	}
    	else if( e == 4) {
    		printf("Enter Half-Time fans: ");
    		scanf("%d", &user_input.ht_fans);
    	}
    	else if( e == 5) {
    		printf("Enter number of parolees: ");
    		scanf("%d", &user_input.parolees);
    	}
    	else if( e == 6) {
    		printf("Enter amount of payments: ");
    		scanf("%f", &user_input.payments);
    	}
    	else if( e == 7) {
    		printf("Enter number of visor tosses: ");
    		scanf("%d", &user_input.visor_tosses);
    	}
    	else if( e == 8 ) {
    		printf("\n\nOpponent:  %s\n", user_input.opp_name);
    		printf("USC's points:  %d\n", user_input.usc_points);
    		printf("Opponent's points:  %d\n", user_input.opp_points);
    		printf("Half-time fans:  %d\n", user_input.ht_fans);
    		printf("Number of parolees:  %d\n", user_input.parolees);
    		printf("Amount of payments:  %.2f\n", user_input.payments);
    		printf("Number of visor tosses:  %d\n\n\n", user_input.visor_tosses);
    		}
    	else if( e == 9 ) {
    		
    		user_input.usc_points = 0;
    		user_input.opp_points = 0;
    		user_input.ht_fans = 0;
    		user_input.parolees = 0;
    		user_input.payments = 0;
    		user_input.visor_tosses = 0;
    	}
    }
    Menu choices 2-10 are working with no problems, but when the user chooses 1, there is no pause for the user to enter a name, it only reprints the menu. If anyone could analyze my code with any hints I would be forever grateful.

    Thanks.
    Last edited by k2712; 08-25-2007 at 11:32 PM.

  2. #2
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Where is the fgets? you posted quite a lot there, 'keep it short' I have read somewhere.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    Sorry about that, about 2/3 of the way down you'll see the function "perform_menu_tasks" with a series of if, else if statements. The fgets is in the first if statement where program goes if the user enters a 1 for the first menu choice.

    Code:
    void perform_menu_tasks( int e ) {
    	
    	if( e == 1 ) {
    		printf("Enter the opponent's name: ");
    		fgets( user_input.opp_name, sizeof (user_input.opp_name), stdin );
    		
                    // Code will be entered here to remove newline.
    	}
    Last edited by k2712; 08-25-2007 at 11:32 PM.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Hmm, I really am at a loss, try adding a
    Code:
    getchar()
    /*just above the printf */
    printf("Enter the opponent's name : ");

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're mixing fgets() with scanf(). Since scanf() typically leaves the newline of your input on the input stream, a following fgets() will return immediately with that single newline (it appears to skip).

    Ideally, convert all those scanf() calls into fgets() + sscanf() pairs.

    There are some methods of clearing the input stream listed in the FAQ.
    Do NOT use fflush(stdin), as this is undefined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    What he said.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    Thank you both for your replies. sea_4_ever, your suggestion worked, even if I'm not sure why.
    Salem, I'll have to look into your reply because I'm not familiar with sscanf, but if I understand correctly, I shouldn't combine fgets and scanf in the same function...

    thanks again

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Heheh
    I don't know why either, but I DO know that fgets does sometimes move ahead too fast, and that getchar helps, Salem just sort of explained why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't fgets() read my input?
    By laczfinador in forum C Programming
    Replies: 10
    Last Post: 05-13-2009, 04:20 AM
  2. fgets not getting user input
    By golgotha in forum C Programming
    Replies: 4
    Last Post: 03-14-2009, 11:27 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 11:06 PM
  5. function and input problems
    By meka in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2001, 11:56 AM