Thread: gets() and scanf() clash?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canterbury,Kent/Colchester,Essex
    Posts
    2

    Angry gets() and scanf() clash?

    Hi there,

    I'm having a problem with the gets and scanf functions. Is there some sort of clash between them?

    My program asks the user to select a type of program usage then enter a string input (roman numerals). The code I have so far is:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    	int selection;
    	char number[10];
    
    	printf("MENU\n 1 - Number Entry Mode\n 2 - Batch Mode\nPlease choose an option...");
    	/*this scanf is messing up the gets below!!!*/
    	scanf("%i",&selection);
    
    	printf("\nPlease insert your number...");
    	gets(number);
    	printf("You entered: %s\n",number);
    }
    This code seems to ignore the gets(number) all together. However if I comment out the scanf it will perform the gets function.

    I've been thinking about it for about a day now and I have no idea why it is doing it.

    Any help would be much appreciated.

    Dave.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could read the FAQ to find out about say
    void main
    using gets()

    etc etc

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    If I recall (and one of the peeps who's stronger on this sort of stuff will probably pop in here shortly), it's to do with the fact that in order for the scanf function to finish you press <Enter>. This puts the newline ('\n') character into the input buffer, but scanf doesn't remove it, just detects it.

    Then, when the gets function looks at the input buffer, straight away it sees the newline and finishes.

    Moral of the story: first, be consistent and safe. Drop the scanf and replace it with:-
    Code:
    fgets(number, 9, stdin);
    sscanf(number, "%i\n", &selection);
    Also replace your last gets statement with the same fgets statement. This ensures that you're not subject to a buffer overflow if the user decides to misbehave.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Canterbury,Kent/Colchester,Essex
    Posts
    2
    That makes sense... I had checked a load of helpfiles/tutorials out and hadn't found out anything about why it wouldnt work.

    I'd seen about the fgets statement, but due to roman numerals only going up to 5000 or so I figured it wasn't really worth it.

    Thanks for the help.

    Dave.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The reason for avoiding gets() has little to do with what you expect the user to input but what the user could input. Yea a reasonable user might not blow your buffer but what about the idiot user? Or even worse the malious user.

Popular pages Recent additions subscribe to a feed