gets() ??

This is a discussion on gets() ?? within the C Programming forums, part of the General Programming Boards category; Hi, I've been working on my code, and I'm running into this problem but I do not know what to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Question gets() ??

    Hi, I've been working on my code, and I'm running into this problem but I do not know what to do with it because I can't seem to find relevent help online.

    Code:
    printf("\nPlease input the word you wish to add (enter any number to exit)");
    		gets(word);
    if (isdigit(word[0])) return; //terminates function if data is numeric
    
    	printf("\nWhere do you wish to add the new node after (0 to add at top)?");
    scanf("%d",&addPlace);
    This is the segment of code which has problem. My question. Does the program pauses after gets(word) to wait for the user to input a string?
    beacause I keep getting an output like this:

    Please input the word you wish to add (enter any number to exit)
    Where do you wish to add the new node after (0 to add at top)?_

    where the underscore is asking for input in the console from the user.

    when I add these code to the bottom of the above code:

    Code:
    printf("string:%s", word);
    printf("\nnumber: %d", addPlace);
    And in the above input place i key in something like "123",

    I get an output of:

    string:
    number: 123

    What should I do? How can I get string inputs reliably? And I have seen suggestions to abandon gets and use fgets? I'm confuse. Thanks for any help given.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Is there a scanf() anywhere above the gets() you're trying? I'm guessing there is. scanf() leaves a \n on the buffer and gets() just figures that the user hit the ENTER key.

    gets() is a horrible function. There's no need to ever use it. You should use fgets() instead.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

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