•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,136 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4125 | Replies: 9
![]() |
hello there, im new in c++...i just want to know how to compare a string to null like:
im using this one, if(&x==0) ...but it does'nt work...
this is what my mentor told me to do...
help..thanks
string x;
cin >> x;
if(x==NULL){
cout << "null";
}
im using this one, if(&x==0) ...but it does'nt work...
this is what my mentor told me to do...
help..thanks
Retreat!!!
•
•
Join Date: Apr 2007
Posts: 102
Reputation:
Rep Power: 2
Solved Threads: 17
string class has no overloaded "==" operator. You can do
string temp;
cin >> temp;
if(temp.empty())
cout << "null";
Given
char temp[100]; I guess you must be talking of how to determine is a string is empty. How about something like if(strlen(temp) == 0) { }. Last edited by ~s.o.s~ : May 10th, 2007 at 10:08 pm.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
>i just want to know how to compare a string to null
You don't. NULL is a null pointer and a string object isn't a pointer. Most likely you want to check for an empty string:
or
And such.
>this is what my mentor told me to do...
Your mentor probably thought you were using C-style strings simulated with pointers. Your sample code isn't though, so that advice is useless.
>string class has no overloaded "==" operator.
Yes, it does. That's one of the selling points of the class.
>i just press enter but it does not alert me that i placed nothing...
Post your full program. We don't like playing 20 questions.
You don't. NULL is a null pointer and a string object isn't a pointer. Most likely you want to check for an empty string:
if ( x == "" )
if ( x.empty() )
>this is what my mentor told me to do...
Your mentor probably thought you were using C-style strings simulated with pointers. Your sample code isn't though, so that advice is useless.
>string class has no overloaded "==" operator.
Yes, it does. That's one of the selling points of the class.
>i just press enter but it does not alert me that i placed nothing...
Post your full program. We don't like playing 20 questions.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
There is no 'null string' as such, only null pointers. A string instance can't be null because it has just been instantiated. And
x != string::npos doesn't make sense either, one is a string object while the other is most probably an unsigned integer. I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Compare String with Strings (Visual Basic 4 / 5 / 6)
- How to compare string value (Java)
- reading input ... quick C++ question ... (C++)
Other Threads in the C++ Forum
- Previous Thread: String class
- Next Thread: Reasonable or not



Linear Mode