Thread: 2 x Scanf (functions)

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    2 x Scanf (functions)

    Whenever I call One scanf it works, then the second one, bypasses the whole scanf, and gives me the default value for it


    how do we get around 2 scanf's?????

    help me someone smart!!!!

    thanks

    greg

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    After every call to scanf(), you must flush the input buffer. Read the board's faq on how to flush the input buffer and why you must do it.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Search the board, many people asked a lot of times.
    anyway some people will tell you to use fgets() and then convert of specified type.

    you can see this here, it will work in a loop:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        char buf[BUFSIZ];
        int num;
        
        while(1) {
            printf("Enter a number: ");
            fgets(buf,sizeof(buf),stdin);
        
            num = atoi(buf); //converts the data
            printf("%d * 10 = %d\n",num,num*10);
        }
        
        system("PAUSE");
        return 0;
    }
    I'm not sure this is the best way, but search the board about the scanf() question, yes I know it's an infinite loop, it was just to show this will work in a loop. CTRL+C or CTRL+Z to stop the program.
    Last edited by Vber; 03-16-2003 at 09:00 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Does any of this help you DrJones? I don't understand your problem, without more details or some sample code.

    gg

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    include <stdio.h>
    
    #define FLUSH_INPUT while( fgetc( stdin ) != '\n' )
    
    main( ) {
    
    	char string[256];
    	int n;
    
    	fscanf( stdin, "%s", string );  // Read in a string
    	FLUSH_INPUT; // Make sure the input buffer is empty
    	fscanf( stdin, "%d", &n ); // Read in a number
    	FLUSH_INPUT; // Make sure the input buffer is empty
    
    	fprintf( stdout, "%s\n%d\n", string, n );
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Replies: 19
    Last Post: 04-04-2009, 08:37 AM
  3. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  4. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM