Like Tree1Likes
  • 1 Post By Matticus

Error when compiling this WHILE statement

This is a discussion on Error when compiling this WHILE statement within the C Programming forums, part of the General Programming Boards category; Code: /* Demonstrates using the gets() return value. */ #include <stdio.h> /* Declare a character array to hold input, and ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Error when compiling this WHILE statement

    Code:
    /* Demonstrates using the gets() return value. */
    
    #include <stdio.h>
    
    /* Declare a character array to hold input, and a pointer. */
    
    char input[257], *ptr;
    
    int main( void )
    {
        /* Display instructions. */
    
        puts("Enter text a line at a time, then press Enter.");
        puts("Enter a blank line when done.");
    
        /* Loop as long as input is not a blank line. */
    
        while ( (*ptr = gets(input)) != NULL)
            printf("You entered %s\n", input);
    
        puts("Thank you and good-bye\n");
    
        return 0;
    }
    Whenever I try to compile the code above I get the error:
    getback.c: In function ‘main’:
    getback.c:18:34: warning: comparison between pointer and integer [enabled by default]
    I just got this from the book that I am using and it is for testing for input of a blank line and readers are being warned in using this format (line 18):
    Code:
    while ( (*ptr = gets(input)) != NULL)
    What is the correct syntax if a gcc compiler is to be used? Thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    First you shouldn't even be using gets. This function is considered very dangerous and should never be used. You should be using a function that limits the number of characters that can be read, like fgets(). Second if you don't understand how to use one of the standard functions you should find and study the documentation for this function. Google is a very useful tool to find documentation for standard C functions.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,572
    What book are you using? I would recommend throwing it in the trash. You aren't supposed to dereference the pointer in the "while()" loop (get rid of the '*') - and "gets()" won't return a NULL if the user enters a blank line.

    And of course, as already mentioned, "gets()" should not be used!
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    First you shouldn't even be using gets. This function is considered very dangerous and should never be used. You should be using a function that limits the number of characters that can be read, like fgets(). Second if you don't understand how to use one of the standard functions you should find and study the documentation for this function. Google is a very useful tool to find documentation for standard C functions.

    Jim
    Hmmm. This is a 2004 self-study book. It also says that the gets() function is dangerous because of many reasons. Do you prefer just the tutorial in this website? Thanks.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    In my opinion tutorials should be an adjunct for a good book. But if your book is teaching gets() and using global variables it should probably be trashed. You may want to check out the book link at the top of this forum.

    Jim

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    I was recently looking for a book for my teenager, so I visited the local B&N. I picked up this one, seemed promising, in that it teaches C++ and the back-of-the-book autor bio is

    Mary Farrell teaches computer science, mathematics, and Japanese at Boston College High School, where she has been teaching for the past 17 years. She is an avid learner of spoken languages as well as computer languages and travels frequently to practice. She lives south of Boston, Massachusetts.
    Well, it seems she's spending too much time learning spoken languages instead of keeping up with computer languages, as I turned to a page of code and found #include <iostream.h>

    Put that one right away, got him a Python book instead.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    Yea, I would say that there are more "bad" books on C/C++ then there are "good" books. Even finding good beginning C tutorials is difficult.

    Jim

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    It's easier to write a bad book, especially if it's aimed at novices (chances are that anyone who knows the subject isn't going to pick up "For Dummies" books about it, for example). By the time they know better, they've already bought it and recommended it to half-a-dozen people.

    Sure, standards shift and whatever, but I still have a C++ For Dummies somewhere that makes some horrendous code (was based on DOS, but I'd bought it in the Windows NT-era!). When you're learning, you don't notice because you think that's what you're supposed to do. As you progress (which 99% of the people who bought the book never will anyway), you learn that the stuff you learned at a beginner isn't ideal. So what? It's only when you know the subject in-depth that you can spot the junk, and by then it's too late - you'll never be in their target audience anyway.

    I am still shocked, though, on how many courses still teach using Turbo-C++. I mean, that was out-of-date before I even owned a PC. And they expect the average teacher to just start teaching programming to kids on Raspberry Pi's et al? Can you IMAGINE the mess that we'll inherit 20 years from now?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Should be while ( (ptr = gets(input)) != NULL)
    No asterisk.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  2. error when compiling if statement
    By robasc in forum C Programming
    Replies: 2
    Last Post: 07-08-2005, 08:20 PM
  3. Compiling error (Maybe a syntax error)
    By davidvoyage200 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2003, 09:09 PM
  4. Error in compiling G++
    By nevermind in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 06:40 AM
  5. Very odd compiling error...
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2002, 09:06 PM

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