Thread: gets? scanf?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    gets? scanf?

    hi,
    new to C and working stuff out...
    the following function is supposed to get a number of strings from user
    however, the output is weird:
    for the first value (i=0) it doesn't wait for input, but goes straight on to i=1.
    Why? when i use scanf this works fine, only problem is i need to get whole sentence from user.
    thanks

    Code:
     
    
    #include "targ46.h"
    
    void GetSentence(int numOfSent,char dictionary[][MAX_WORD],
    				 int duplicates[MAX_DICT])
    {
    	int i;
    	char sentence[MAX_SENTENCE];
    	for (i=0; i<numOfSent;i++)
    	{
    		printf("Sentence %d:\n",i+1);
    		gets(sentence);
    		ProcessSentence(sentence,dictionary,duplicates);
    	}
    }
    Last edited by msshapira; 01-07-2009 at 03:42 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because you have a newline sitting in the input buffer.

    And gets() is probably the worst ever function in the whole C library, as if someone types in more than MAX_SENTENCE characters, your program will crash, and there is absolutely nothing you can do to prevent it aside from not using gets(). fgets() can be used as a replacement - it is safe as long as you don't lie about the size of the string you store the input into.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    It could well be that there was a '\n' (newline) left in the buffer from some previous input. As for gets, don't use that - it cannot be made safe. Read this.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    how can I empty the input buffer? A dummy scanf? of in the previous scanf("%d") should it be changed to scanf("%d\n");
    I'm using gets coz its part of an assignment, havnt got to reading from files yet....
    the assignment assumes good input.
    thanks

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Have a look here for a way to clear the input buffer. It is understandable if your teacher requires you to use certain functions, that you will have to use them even if they are bad. It's not a very comforting thought, but your teacher may not always want you to do things the best way.
    Last edited by kermit; 01-07-2009 at 03:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM