How come I can't compare a string token?

This is a discussion on How come I can't compare a string token? within the C++ Programming forums, part of the General Programming Boards category; I made this program that tokenizes a date based on the "/" character. However, the test string below the if ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How come I can't compare a string token?

    I made this program that tokenizes a date based on the "/" character. However, the test string below the if statment is never executed. How come? There are two 12's in my date, it should print out the test string twice.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void checkDate( char * );
                    
    
    int main(){
        
        char date[] = "12/12/2005";
        checkDate( date );
        
        return 0;
    }
    
    void checkDate( char * x ){
         char * token;
         char test[] = "12";
         token = strtok( x, "/" );
         
         while( token != NULL ){
                cout << token << endl;
                if( token == test )
                    cout << "this is a test string" << endl; // how come I don't see this output???
                token = strtok( NULL, "/" );
         }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    You cannot compare C style strings with ==. You should probably switch to the string class if you are using C++. If you want to/have to use C style strings, compare them with strcmp.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    49
    Is it possible to tokenize string classes based on a delimiting character? How would I go about doing this?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Yes, std::stringstream can be used with std::string.
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,348
    You could use an existing tokenizer (I think Boost has one), or you can use a combination of find and substr. If you have more than one delimiter, find_first_of or find_first_not_of is more appropriate than find itself. Just run it in a loop while the result of find is not string::npos, and remember to start from the last place you left off and you can get each token.

    There are probably a few examples if you search these forums.

    And the stringstream will work if you have only a single character delimiter to use with getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 07:01 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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