Thread: fgets and exiting with a certain condition?

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    fgets and exiting with a certain condition?

    Code:
    #include <stdio.h>
    int main()
    {
        int line;
        char userInput[6][160];
        for (line = 0 ; line < 6; line++)
            fgets(userInput[line], 160, stdin);
        if (fgets(userInput[line], 6, stdin) == NULL)
        {
            break;
        }
    
    
        for (line = 0 ; line < 6; line++)
            printf("line %d: %s\n", line, userInput[line]);
    }
    when i input 2 'hello' s


    hello
    hello
    <ENTER> <- blank line
    <ENTER> <- blank line
    <ENTER> <- blank line
    <ENTER> <- blank line


    what i'm trying to figure out is if the user doesn't enter anything in the 3rd line i want it to print out the line they left blank and return the input, is there a way of doing this?

    In the above code i have:

    Code:
      if (fgets(userInput[line], 6, stdin) == NULL)
        {
            break;
        }
    What i attempted here is if the line contains nothing then it should break out and exit. It doesn't seem to work though, it just keeps asking for input as usual without it

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know, if you actually paid attention once in a while, you wouldn't have to keep asking questions. How about you go actually read how a function works once in a while? Huh? You just come up with some bizarre, completely wrong idea of how to do something, when people keep telling you over and over the right way to do it. It doesn't matter though, you just ignore everything you're told.

    Ok, let's pretend you're not a troll, and look at your code:
    Code:
    #include <stdio.h>
    int main()
    {
        int line;
        char userInput[6][160];
        for (line = 0 ; line < 6; line++)
            fgets(userInput[line], 160, stdin);
        /* You are no longer in your loop. */
        /* So what exactly is 'line' now? */
        /* Here's a hint, since you don't know. It's 6. */
        /* 6 is not a valid accessable line in your array. */
        if (fgets(userInput[line], 6, stdin) == NULL)
        {
            break; /* Why are you breaking? You're not in a loop. */
        }
    
        for (line = 0 ; line < 6; line++)
            printf("line %d: %s\n", line, userInput[line]);
    }
    Even if you were in a loop, why would you be calling fgets again? See, you don't pay attention to anything anyone tells you. You don't even try to understand how the function works, or what that function does. You just throw it in randomly and then wonder why it doesn't work.

    It doesn't work, because you don't pay attention.


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

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To add: fgets() worked off of "cooked" input, meaning the system gets the input then gives it to your program. The system is ignoring those blank lines so your program never even sees them. To do what you want you'll have to use a "raw" input method.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    In this case those "blank" lines aren't exactly blank.

    In the OP example he pressed <ENTER> for a blank line, so
    the newline character would be available for fgets and would be stored in userInput[line].

    So he could compare the current line of input to "\n" and probably get close to what he wants

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Ok after reading all your suggestions heres the new code:
    Code:
    #include <stdio.h>
    int main()
    {
        int line;
        char userInput[6][160];
        for (line = 0 ; line < 6; line++)
        {
           //     get the input 
            
            fgets(userInput[line], 160, stdin);
          // if the row equals a new line, then we'll break out of it.
    
            if (userInput[line] == "\n")
            {
     
               printf("It's a blank line");
               break;
            }
        }
    
    
        for (line = 0 ; line < 6; line++)
        printf("line %d: %s\n", line, userInput[line]);
    .. i still have the same problem in my original post.
    Last edited by Axel; 09-13-2005 at 08:58 AM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    that's better.

    look into strcmp() for testing "\n". You cannot you == to compare strings

    Also, you only want to print as many lines as you have read in.
    Last edited by spydoor; 09-13-2005 at 09:08 AM.

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm... tricky... it just accepts 1 line as input and then it exists, regardless of the break in my if statement. Is it because it encounters a '\n' at the end of each row? therefore it exists after it accepts the first line of input?

    Code:
     
    
    
        for (line = 0 ; line < 6; line++)
        {
            fgets(userInput[line], 160, stdin);
            if (strcmp(userInput[line], "\n"))
            {
               printf("\nIt's a blank line");
               break;
            }
        }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    This is an example of where you should have read more about a function new to you, or at least tried various methods

    strcmp returns 0 if the strings match, non-zero if they don't, so your test is backwards.

    you should have something like
    Code:
    if(strcmp(userInput[line], "\n") == 0) //user entered a blank line

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I forgot that fgets() does capture blank lines.

    But of course if all you are looking for is if its a blank line, then there is no need for strcmp

    Code:
    if (userInput[line][0] == '\n')
    since you know there won't be anymore useful data after the '\n'

  10. #10
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Thanks for the help guys :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem exiting program
    By sweetorangepie in forum C Programming
    Replies: 17
    Last Post: 03-02-2008, 02:15 PM
  2. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  3. the simplest problem of all (about fgets)
    By white in forum C Programming
    Replies: 5
    Last Post: 11-02-2005, 12:00 PM
  4. fgets error handling
    By mlsbbe in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 02:45 PM