Thread: printf+gets inside of code

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14

    printf+gets inside of code

    I need to read two strings from stdin (keyboard) and save them to two char-variables.
    I do not know why when I use "gets" on the beginning of the code it is function OK:
    Code:
    printf("Enter Date Time in format \"Jun 21 04\": ");
    gets(date);
    printf("Enter COM number (two digits): ");
    scanf("%2s", num);
    But when I change order of scanf and gets, this way:
    Code:
    printf("Enter COM number (two digits): ");
    scanf("%2s", num);
    printf("Enter Date Time in format \"Jun 21 04\": ");
    gets(date);
    than program not stops after "Enter Date Time in format "Jun 21 04":" User cannot put second string.

    Could exist some solution for reading of string inside (not on the beginning) of code? I tried to use brackets {} but it does not help.
    Thank you for your help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is you're using two different methods of getting input. Mixing the two has this effect you're getting. You have two problems:
    1) gets is unsafe and should never be used. There's a FAQ on the site that explains more, and chances are if you have anything other than an ancient compiler, it's already warning you against using it.
    2) scanf leaves the newline character in the input stream, and so it's there for whatever your next input call is. In your case, gets reads this, sees it's a newline, and stops looking for anything else. Because that's just how gets behaves. It only stops on a newline. It doesn't stop for anything else.


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

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, its the common mistake that all the newbie does while programming things. the first mistake that u have done is using gets fucntion to read string which is actually a very bad programming practice. the problem which u are facing because when u use scanf before any gets or fgets fucntion u find this problem that is because the scanf fucntion leaves the '\n' in the keyboard buffer it self, and when the fgets function looks into buffer it sees the '\n' key and assumes that the user had enter the value before and just ignores that part.

    the solution to this problem is when ever u use scanf and the fgets u will have to clear the input buffer before calling fgets. the follwing single line statment may give u the solution
    Code:
    int ch;
    
    while((ch=getchar())!='\n' && ch!=EOF);
    ssharish2005

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    If you put a space at the end of your format string for scanf - so change it to scanf("%2s ", num) - it will match any trailing whitespace, invluding a newline, so the \n will not be left in the stream.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > it will match any trailing whitespace, invluding a newline, so the \n will not be left in the stream.
    Except it stops scanf from returning at all until you type in another (non-white space) character.
    Stick to fgets() for all input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    fgets() and sscanf() is better than just using scanf(), I agree.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Europe Cz Prague
    Posts
    14
    Thank you very much for your explanation and help.
    I successfuly used clever solution from 'ssharish2005' with clear input buffer before 'gets'.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, we have FAQs full of such clever solutions.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 12:50 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM