Thread: fgets prompt in while loop "skips"

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Jesdisciple View Post
    Alright, I reverted the other bugfix to test this one, but the output is the same as it was at the beginning... Is my condition giving a bad "false" result?
    Code:
    	fgets(input, (int)length, stdin);
    	if(strlen(input) == length && input[length - 1] != '\n'){
    		while ((ch = getchar()) != '\n' && ch != EOF)
    			/* Clean out the buffer. */;
    	}
    EDIT: Never mind; I needed to subtract 1 from length both times. Thanks again!
    There is no reason to check if strlen(input) == length - justcheck if input[strlen(input)-1] != '\n' - it's not really important to only do this when you have strlen(input) == length-1, since the expensive part of the check is to do strlen - so we may just as well simply compare the end of the string with newline. It makes it ONE check, rather than two, making the code simpler.

    I personally would probably just set a max line length of some rather large number, say 256 [assuming you do not have some really long input expected, in which case a larger number is appropriate]. If you do that, you can probably ignore checking for newline in the input string [although you probably will, one way or another deal with the fact that there is a newline at the end of the string - however, that is a slightly different matter].

    --
    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.

  2. #17
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    The reason I didn't do that is that the string may be shorter than the char *. For example, say I had a third option "maybe." Then I need to ask fgets for 7 characters, and the response might be "no":
    [n][o][\n][\0][?][?][?]
    And then the next-to-last character is probably not a newline, and the user is confused that the program is hanging on an empty line...

    EDIT: DUH! I'm sorry, you're right and I'm contradicting myself...

    EDIT2: ... And I forgot to reply to the rest of your message... I know modern computers aren't hurting for RAM, but I prefer to only ask for what I'll use.
    Last edited by Jesdisciple; 04-12-2009 at 07:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. need to loop my prog. after the prompt
    By d-dubb in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 01:39 PM