Thread: trouble with checking a string..

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    trouble with checking a string..

    I want to check to see if "new_name" is not blank. How can I do this?
    Thanks in advance!
    JK

    Code:
    strncpy(new_name, old_name.c_str(),4);
    
    if (new_name != "")  
    {
      // do some stuff here
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    There is a function strcmp() in the same library as strncpy().
    http://www.cplusplus.com/ref/cstring/strcmp.html


    You might also look into using STL strings, as you can directly compare them.

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Strings end with a null pointer, so you could check to see if the first element in the string is not '\0'.

  4. #4
    Banned
    Join Date
    Oct 2004
    Posts
    250
    You could use sizeof
    Code:
    if (sizeof(buffer) <= 0)
    	{
    		// string is empty
    	}

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by cgod
    You could use sizeof
    Code:
    if (sizeof(buffer) <= 0)
        {
            // string is empty
        }
    What are you on cgod??? That will NEVER work.

    The best way to see if a string contains nothing would be to just either test if the \0 is at index 0, or if the length of the string is 0, using strlen.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    sizeof would not work. new_name could be dynamic, and sizeof would simply return you the size of a char * . Use strcmp as AH_Tze said, trying to check the first element is not safe, if the string is dynamic of course.

    If it's not, this change would fix your code :
    Code:
    if (new_name[0] != "")   // or new_name[0] == '\0', new_name[0] == NULL
    {
       // do some stuff here
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could also do this:

    Example:
    Code:
    if(!old_name.empty())
    {
    
    }

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    82
    master5001 may have the best solution.

    I'm slightly confused as to what the purpose of the code was. But if you only need the first 4 char's of a string or some such if it's not empty, checking it as a string could be good.

    If I was skimming your code for errors, potential errors, or spots that would be dangerous to change, I'd want to completely grasp the flow of control. For that I'd need to know exactly what happens in that 'if' statement.
    If the string conversion and manipulation was inside the conditional and is working okay, I wouldn't really need to fully understand it. And as functions like strncpy have complicated pre and postconditions (is there a null?, what if there's too many/too few chars etc.), hiding the complexity can be helpful. And it doesn't get much simpler than if(!str.empty()).
    Last edited by AH_Tze; 02-09-2005 at 02:46 AM.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by AH_Tze
    master5001 may have the best solution.

    I'm slightly confused as to what the purpose of the code was. But if you only need the first 4 char's of a string or some such if it's not empty, checking it as a string could be good.
    no, in this case master5001's solution causes a syntax error. at this point, new_name is a char*, not an std string. the best thing would be to check the first character against '\0' to make sure it has something in it. his code may be working exactly the way he wants it to.

    the purpose of the code (or at least the way it was written) is to take up to the first four characters from a string (maybe used for easy input) and put them into a char* (maybe used for binary file output). then they want to check and make sure the char* points to something useable before performing some action (presumably having to do with that something that he so recently checked)
    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

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (sizeof(buffer) <= 0)
    LOL - sizeof on any real object you can do sizeof on returns at least 1
    Condition is always false.
    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.

  11. #11
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by skorman00
    trying to check the first element is not safe, if the string is dynamic of course.
    Can you elaborate on this? I fail to see what difference it makes, whether the string is dynamicly or staticly bound.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    nothing has worked so far. New name is a char[10]. old_name is a std::string.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if it's a std::string, then this should be fine

    Code:
    if ( old_name != "" )
    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.

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by jerrykelleyjr
    nothing has worked so far. New name is a char[10]. old_name is a std::string.
    so you're telling me you've tried
    Code:
    if(new_name&#091;0&#093;=='\0')
    {
        //executes if the new_name string is empty
    }
    and it didn't work?
    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

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    82
    @ jerry

    Post more code. It's difficult to write and check solutions while only looking at a piece of it. Maybe not the entire program, but at least something that we could compile. One test is worth a thousand expert opinions.

    @major_small
    no, in this case master5001's solution causes a syntax error. at this point, new_name is a char*, not an std string.
    He used old_name, which is a string. Still think it may be the best solution.
    Last edited by AH_Tze; 02-09-2005 at 02:05 PM.

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