Thread: trouble with checking a string..

  1. #16
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Scribbler:
    I fail to see what difference it makes, whether the string is dynamicly or staticly bound.
    It is not safe if the string is dynamic, because new_name may be NULL. If that is the case, new_name[0] would result in a seg fault.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I was able to use strlen. Thanks to all for your help!

    Code:
    int len;
    
    len = strlen(new_name);
    
    if (len >0)
    {
      // do some stuff
    }

  3. #18
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by AH_Tze
    He used old_name, which is a string. Still think it may be the best solution.
    read the very first post...
    Code:
    strncpy(new_name, old_name.c_str(),4);
    
    if (new_name != "")  
    {
      // do some stuff here
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #19
    Registered User
    Join Date
    May 2003
    Posts
    82
    how's that?

    If c_str() is a member function of old_name, that would imply it's an STL string. Thus you could call empty() on it.

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    c_str() returns the C style string buried in the implementation of the C++ string object.
    Code:
    string s = "dodah";
    char cString[10];
    if(s.length() < 9)
      strcpy(cString, s);
    cout << cString << end;
    You're only born perfect.

  6. #21
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by AH_Tze
    how's that?

    If c_str() is a member function of old_name, that would imply it's an STL string. Thus you could call empty() on it.
    yeah, we know that, but did you even read the code? he's checking against new_name, which is a char*

    edit: to be more specific, I ran this code:
    Code:
    #include<iostream>
    #include<cctype>
    
    using namespace std;
    
    int main()
    {
        string old_name;
        string new_name;
        char*nname=new char[4]; //used for testing
        
        strncpy(new_name, old_name.c_str(),4);  //this throws an error
    
        if(nname[0]=='\0')  //I know this is wrong, but this was for testing
        {
            cout<<"empty";
        }
        
        cin.get();
        return 0;
    }
    and got this error:
    Code:
    no matching function for call to `strncpy(std::string&, const char*, int)'
    however, when I run the code I suggested:
    Code:
    #include<iostream>
    #include<cctype>
    
    using namespace std;
    
    int main()
    {
        string old_name;
        string new_name;        //used for testing
        char*nname=new char&#091;4&#093;;
        
        strncpy(nname, old_name.c_str(),4);  //this works now
    
        if(nname&#091;0&#093;=='\0')  //now this is right, and all works perfectly
        {
            cout<<"empty";
        }
        
        cin.get();
        return 0;
    }
    I get no errors. in fact, I get exactly what I'm expecting - a console window with the single line:
    Code:
    empty
    Last edited by major_small; 02-09-2005 at 11:39 PM. Reason: details and CODE TAGS!!!
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    82
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int main()
    {
        string old_name;
        // presumably assign old_name somewhere in here.
           
        if(!old_name.empty()) // easy to read conditional.
        {
            char* new_name = new char[4];
            strncpy(new_name, old_name.c_str(), 4);
            // do what you got to do with a non-empty c-string
            // All the messy c-string work is isolated here.
            
            cout << new_name << endl;
        }
        else
        {
            cout << "empty" << endl;
        }
        
        cin.get();
        return 0;
    }
    Last edited by AH_Tze; 02-10-2005 at 12:28 AM. Reason: forgot a few lines

  8. #23
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ah, I see what you meant... I generally try not to change the logic of the OP, because, for example, he may have intended to copy the string into the array wether or not it was empty. for example, if this code is being used to change/remove a password, your version would require that something was put in, where the OP code may have thrown a warning and continued on it's merry way with it's empty char*.

    in this case, however, it probably doesn't matter.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #24
    Registered User
    Join Date
    May 2003
    Posts
    82
    Quote Originally Posted by major_small
    I generally try not to change the logic of the OP
    bah, respecting original intent is so old school. Subversion is the new paradigm.

    /me notices frustrating lack of evil smileys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM