Thread: error with custom "chomp" function

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    error with custom "chomp" function

    Code:
    bool chomp(char *s)
    {
    	for(int i = 0;s[i] != '\0'; i++)
    	{
    		if(s[i] == '\n')
    		{
    			s[i] == '\0';
    			printf("chomp returned true\n\n");
    			return true;
    		}
    	}
    	printf("chomp returned false\n\n");
    	return false;
    }
    (the printf's are for debugging)

    the function returns true, yet still doesn't do it's job. I've looked through many times, and can't see what's wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > s[i] == '\0';
    Turn up your compiler warnings until this says 'useless comparison'
    Then change the == to an =
    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.

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Oh! I didn't notice that... thx.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Assuming you are compiling in C++

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yeah, now you mention it, it is C++ code
    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
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    Yeah, now you mention it, it is C++ code
    OK, I'll bite. What makes this C++?

    [edit]Oh! the bool.
    Not if it was typdefed
    [/edit]
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What makes this C++?
    The fact that we choose to ignore C99 until it is better implemented.
    My best code is written with the delete key.

  8. #8
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    actually...

    It's D code. But since this time nothing here was D-only, I posted it here.

    Doesn't C have a bool with:

    Code:
    typedef int bool;
    #define false 0
    #define true 1
    ?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Doesn't C have a bool with:
    Yes, but when you post code that assumes such a declaration, you should include it in your thread. That avoids confusion. However, the cry of C++ (or C99) was actually based on this line:
    Code:
    for(int i = 0;s[i] != '\0'; i++)
    Because C89 doesn't allow a declaration in the initialization part of a for loop. Some compilers support this feature by extension because it's useful, but it wasn't actually standardized until C99. C++ also supports this feature.
    My best code is written with the delete key.

  10. #10
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Ohh...

    I remember that, and that C++ can do that. Same how C++ can declare variable's 'on-the-fly', while in C you have to do so at the begining of the function (which I still do anyways).

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    typedef int bool;
    #define false 0
    #define true 1
    I don't like that way it waste space. I use
    Code:
    typedef enum { BFALSE, BTRUE } bool_t;
    Thanx Prelude for that one

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't like that way it waste space. I use
    No, your way wastes space if you actually think about it. There is only three (five if you count the newline) characters difference in the two methods.

    If you use your method, once each, you actually use more space than the previous. Here's how:
    If you use "BFALSE" and "BTRUE" each once:
    Code:
    bool_t something( void )
    {
        if( rand() % 2 )
            return BFALSE;
        else
            return BTRUE;
    }
    You actually use one more character than without using your enum. And seriously, if you're going to bother creating a new data type, you can be pretty sure they're going to use it more than once each.

    As such, every time beyond one, you use 1 more character for each "BTRUE", one more for each "BFALSE", and two more for each "bool_t".

    So if you want to be really picky, your way takes more space.

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

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >As such, every time beyond one, you use 1 more character for each "BTRUE", one more for each "BFALSE", and two more for each "bool_t".
    However, some compilers will complain if you use "true" or "false" or "TRUE" or "FALSE" for the named constants because of conflict with other named constants. Likewise with a typedef of bool. The code given by linuxdude (which was originally written by me) makes a weak attempt to avoid clashes.

    So if you want to be really really picky, your way uses fewer keystrokes, but my way is more robust. Of course, because of the problems above, it really is better to avoid declaring your own bool type in C89, and use the supported bool type in C++ and C99.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM