Thread: stopping in else block

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    24

    stopping in else block

    could someone please explain to me why this function is stopping before the fgets inside the else block. After it prints the first message after the else it jumps to the system() call after the else block.

    Code:
    void config(const char options[])
    {
    	char default_config[] = " --prefix=/usr --disable-nls";
    	char custom_config[BUFSIZ];
    	char whole_config_line[BUFSIZ];
    	char *p;
    
    	strcpy(whole_config_line, "./configure ");
    
    	if( ( strcmp(options, "default") == 0)){
    		strcat(whole_config_line, default_config);
    	} else {
    
    		printf("please give configure options: \n");
    	/* stopping here. why? */
    		fflush(stdout);
    		fgets(custom_config, sizeof(custom_config), stdin);
    		p = strchr(custom_config, '\n');
    		*p = '\0';
    		strcat(whole_config_line, custom_config);
    	}
    
    	system(whole_config_line);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because you have a newline still in your buffer (stdin), so it reads that, and continues on its way.
    Code:
    while( fgetc( stdin ) != '\n' );
    fgets(custom_config, sizeof(custom_config), stdin);
    Try that. Usually you want to check for EOF there too, but since in your case you're probably not entering that ever, this should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    24
    that works. Thank you very much.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by linuxman
    that works. Thank you very much.
    Except for the possibility if there is nothing in the input buffer -- by calling this routine from somewhere else in your program. quzah's while() code should be placed after the input line that left the offending \n in the buffer.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM