Thread: trouble with checking a string..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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

  3. #3
    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