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 }
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.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()).
Last edited by AH_Tze; 02-09-2005 at 02:46 AM.
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.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)
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
> 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.
Can you elaborate on this? I fail to see what difference it makes, whether the string is dynamicly or staticly bound.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 != "" )
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.
so you're telling me you've triedOriginally Posted by jerrykelleyjrand it didn't work?Code:if(new_name[0]=='\0') { //executes if the new_name string is empty }
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
@ 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.no, in this case master5001's solution causes a syntax error. at this point, new_name is a char*, not an std string.
Last edited by AH_Tze; 02-09-2005 at 02:05 PM.