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
}
Printable View
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
}
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.
Strings end with a null pointer, so you could check to see if the first element in the string is not '\0'.
You could use sizeof
Code:if (sizeof(buffer) <= 0)
{
// string is empty
}
What are you on cgod??? That will NEVER work.Quote:
Originally Posted by cgod
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.
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
}
You could also do this:
Example:
Code:if(!old_name.empty())
{
}
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()).
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.Quote:
Originally Posted by AH_Tze
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)
> if (sizeof(buffer) <= 0)
LOL - sizeof on any real object you can do sizeof on returns at least 1
Condition is always false.
Can you elaborate on this? I fail to see what difference it makes, whether the string is dynamicly or staticly bound.Quote:
Originally Posted by skorman00
nothing has worked so far. New name is a char[10]. old_name is a std::string.
Well if it's a std::string, then this should be fine
Code:if ( old_name != "" )
so you're telling me you've triedQuote:
Originally Posted by jerrykelleyjr
and it didn't work?Code:if(new_name[0]=='\0')
{
//executes if the new_name string is empty
}
@ 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
He used old_name, which is a string. Still think it may be the best solution.Quote:
no, in this case master5001's solution causes a syntax error. at this point, new_name is a char*, not an std string.