User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2006
Location: Oblivion
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

how to compare a string to null...

  #1  
May 10th, 2007
hello there, im new in c++...i just want to know how to compare a string to null like:

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!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: mariocatch is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: how to compare a string to null...

  #2  
May 10th, 2007
string class has no overloaded "==" operator. You can do

      string temp;
      cin >> temp;
 
      if(temp.empty())
            cout << "null";
 
Reply With Quote  
Join Date: Jul 2006
Location: Oblivion
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: how to compare a string to null...

  #3  
May 10th, 2007
uhmm, what about the;

char temp[100];

how would compare to null...

btw, thank you...
Retreat!!!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: how to compare a string to null...

  #4  
May 10th, 2007
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.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,419
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 98
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: how to compare a string to null...

  #5  
May 10th, 2007
Does it work in C++ like this?:

 if(  *temp_ptr == 0 ) {}
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Jul 2006
Location: Oblivion
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: how to compare a string to null...

  #6  
May 10th, 2007
its not working, i just press enter but it does not alert me that i placed nothing... T_T
Retreat!!!
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: how to compare a string to null...

  #7  
May 11th, 2007
>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:
if ( x == "" )
or
if ( x.empty() )
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: how to compare a string to null...

  #8  
May 11th, 2007
> i just press enter but it does not alert me that i placed nothing.

Maybe the newline is getting the best of you...
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: kylcrow is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: how to compare a string to null...

  #9  
May 11th, 2007
In c++ you can compare a "null string" by going (x != npos) Cant you?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: how to compare a string to null...

  #10  
May 11th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC