SomeClass::SomeClassConstructor()
{
string = new char[ 50 ];
}
SomeClass::SomeClassgetString()
{
if ( string == 0 )
cout << "String is empty";
else
cout << string << endl;
}
empty strings....
you can not test if a string is empty by comparing it 0. Try using the int strlen(char*) function. example:
for a little more of an explination:
when you define a char* and set it to new, you give that variable (pointer) a value which is the memory adress of that new memory. when you do if(str == 0), you are testing to see if that memory's address is 0.
Hopefully that made some sence... You might want to do some reasearch on pointers for more info.
Dwiel
if(strlen(str) == 0) // str is empty
for a little more of an explination:
when you define a char* and set it to new, you give that variable (pointer) a value which is the memory adress of that new memory. when you do if(str == 0), you are testing to see if that memory's address is 0.
Hopefully that made some sence... You might want to do some reasearch on pointers for more info.
Dwiel
hmmm. I tried that, but it doesn't work....
this is what I have:
void viewChar( PlClient &seeObj )
{
if ( strlen( seeObj.plName ) == 0 )
cout << "A name is not assigned\n";
else
cout << seeObj.plName << endl;
}
This is what happens:
this is what I have:
void viewChar( PlClient &seeObj )
{
if ( strlen( seeObj.plName ) == 0 )
cout << "A name is not assigned\n";
else
cout << seeObj.plName << endl;
}
This is what happens:
I don't know how it works in C++, but in C when you declare a variable, it always hold something. The best way to ensure a string to be empty is to assign an empty string("") or NULL.
ahh. finally I got it. I declared plName as a NULL like:
*plName = NULL;
if I left it as
plName = NULL;
would that mean I'm telling plName to have no address? but when I do: *plName = NULL, I'm telling it to be an empty string?
*plName = NULL;
if I left it as
plName = NULL;
would that mean I'm telling plName to have no address? but when I do: *plName = NULL, I'm telling it to be an empty string?
For one thing, after you allocate new memory (with "new char[50]" in your case), the contents are not zero; they're just random garbage. For another, even if the string was empty, the easiest way to check is this:
Note that this assumes 'string' has been allocated. If, on the other hand, the string pointer is NULL (not allocated), which is not the same as being a valid, empty, string, then you'd use your original code "string == 0".
Anyway, after you create the string, add this:
That sets the first character to 0. Strings are null-terminated so calling cout on it now will print nothing; it's an empty string.
if( string[0] == 0 ) cout << "string is empty" << endl;
Note that this assumes 'string' has been allocated. If, on the other hand, the string pointer is NULL (not allocated), which is not the same as being a valid, empty, string, then you'd use your original code "string == 0".
Anyway, after you create the string, add this:
string[0] = 0;
That sets the first character to 0. Strings are null-terminated so calling cout on it now will print nothing; it's an empty string.
Quote:Original post by TheOne1
ahh. finally I got it. I declared plName as a NULL like:
*plName = NULL;
Which, if plName hasn't been allocated yet, would usually cause a crash, or a memory error that causes the program to crash later on.
Quote:
if I left it as
plName = NULL;
would that mean I'm telling plName to have no address?
Yes.
Quote:but when I do: *plName = NULL, I'm telling it to be an empty string?
Yes, that's the same as saying 'plName[0] = NULL', but this only works if the string has already been assigned memory.
Quote:Original post by TheOne1
This is what happens:
Simple, the memory you allocate is not initialised to zero and contains random gibberish. Add a "strcpy (plName, "");" to the constructor.
Better yet, simply use std::string, as suggested by Zahlman.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement