Thread: Error comparing char*

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    Error comparing char*

    If I go

    Code:
    char* input_char = "";
    while(input_char != "exit") cin >> input_char;
    it never exits the loop, even if I type exit. If I cout input_char every time, it looks like it should work.

    Code:
    while(input_char != "exit\0") cin >> input_char;
    doesn't work either, nor does

    Code:
    while(input_char != (char*)"exit") cin >> input_char;
    or

    Code:
    while(input_char != (char*)"exit\0") cin >> input_char;
    I have no idea why. The only way I have found to make this work properly is to treat input_char as an array and go

    Code:
    while(!(input_char[0]=='e'&&input_char[1]=='x'&&input_char[2]=='i'&&input_char[3]=='t'&&input_char[4]=='\0') cin >> input_char;
    I've also tried casting input_char to an (int) and comparing it to the (int) version of "exit", but for some reason the (int) version of input_char is the same, no matter what my input.

    I've been having this problem for a while and I'm not sure why.. why?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use strcmp(). Better yet, use std::string, upon which your attempted comparison will work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    You cannot compare character litterals with the == operator. You have to either use strcmp(), strncmp() or use std::string.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    I like using built-in functions as little as possible. I've also found that using strings makes things a lot harder in bigger programs. If all else fails, I'll have to though.

    So if I compare each individual character it'll work, but not otherwise? that's weird..

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Err... Using strings makes things a whole lot easier in bigger programs. Unless you have specific needs or you are obligated to (by using a library to forces you to, for example) using std::string is *always* a better/easier/safer option than using an array of char or a pointer to a char.

    Edit: Not to mention that using the standard functions as little as possible is quite the opposite of what you would want to do since they are most probably *a lot* better than any function you could write.

    Code:
    bool mystrcmp(const char* str1, const char* str2)
    {
        if(strlen(str1) != strlen(str2)) return false;
        do
        {
            if(*str1 != *str2) return false;
        } while(str1++, *(++str2) != '\0');
        return true;
    }
    Last edited by Desolation; 01-06-2008 at 05:47 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I like using built-in functions as little as possible.
    What is your reasoning?

    If you are aiming to learn by reinventing the wheel, then write your own strcmp function (or string class) and use it, or better yet, just switch back to using what is provided. If not...

    I've also found that using strings makes things a lot harder in bigger programs. If all else fails, I'll have to though.
    ... you'll never be able to write bigger programs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by RobotGymnast View Post
    I like using built-in functions as little as possible. I've also found that using strings makes things a lot harder in bigger programs. If all else fails, I'll have to though.

    So if I compare each individual character it'll work, but not otherwise? that's weird..
    It's not weird at all. What you are comparing is the pointers' address thus it will *always* return false.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    ah.. But it doesn't really act as a pointer, outputting it does not output an address, and it can be used as an array of chars, and it never uses the * operator (like pointers do to assign values into what they point at).

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But it doesn't really act as a pointer
    Yes, it does. If the output function looks at what it points to, that's the output function's business.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    It *is* a pointer. It does not output an address because ostream (cout) has overloaded the << operator for char*. You can use the dereference operator ( * ) to assign values.

    Code:
    char array[5 + 1] = { 0 };
    char* ptr = array;
    *ptr = 'H';
    *(ptr + 1) = 'e';
    *(ptr + 2) = 'l';
    *(ptr + 3) = 'l';
    *(ptr + 4) = 'o';

Popular pages Recent additions subscribe to a feed