Newbie on while condition need help

This is a discussion on Newbie on while condition need help within the C Programming forums, part of the General Programming Boards category; Dear, I am a newbie on C and have a question. The code is on below, Code: while ((n<MAXLINES) && ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Question Newbie on while condition need help

    Dear,

    I am a newbie on C and have a question. The code is on below,

    Code:
    while ((n<MAXLINES) && (gets(buffer) != 0) && (buffer[0] != '\0'))
    I know that the gets fuction will get the input from my keyboard and store into "buffer".

    If I just simply hit enter without any char input, the while condition will not run because the enter key will send the \0 to 'buffer'. Am I right?

    The complete code is below which let me catch no ball.

    Code:
    int get_lines(char *lines[])
    {
       int n = 0;
       char buffer[80];
       
       puts("Enter one line at time; enter a blank when done.");
    
       while ((n<MAXLINES) && (gets(buffer) != 0) && (buffer[0] != '\0'))
       {
           if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
               return -1;
           strcpy (lines[n++], buffer);
       }
       return n;
    }
    Thx mate

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,597
    I suggest that you read why gets() is bad.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Works for me, what problem are you seeing?

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

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    Just want to know what happened if I just hit enter insteed of string.
    Last edited by mavewei; 02-14-2008 at 03:52 AM.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by mavewei View Post
    Just want to know what happened if I just hit enter insteed of string.
    It works, and exits with a 0 status.
    The informative message from my C compiler:
    getlines.c: warning: the `gets' function is dangerous and should not be used.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  3. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  4. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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