| | |
how to compare a string to null...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
C++ Syntax (Toggle Plain Text)
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: 103
Reputation:
Solved Threads: 17
string class has no overloaded "==" operator. You can do
string temp;
cin >> temp;
if(temp.empty())
cout << "null"; uhmm, what about the;
how would compare to null...
btw, thank you...
C++ Syntax (Toggle Plain Text)
char temp[100];
how would compare to null...
btw, thank you...
Retreat!!!
>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:
C++ Syntax (Toggle Plain Text)
if ( x == "" )
C++ Syntax (Toggle Plain Text)
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 here to prove you wrong.
![]() |
Similar Threads
- How to compare string value (Java)
- Compare String with Strings (Visual Basic 4 / 5 / 6)
- reading input ... quick C++ question ... (C++)
Other Threads in the C++ Forum
- Previous Thread: String class
- Next Thread: Reasonable or not
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






