how to compare a string to null...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

how to compare a string to null...

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

  1. string x;
  2. cin >> x;
  3.  
  4. if(x==NULL){
  5.  
  6. cout << "null";
  7.  
  8. }

im using this one, if(&x==0) ...but it does'nt work...
this is what my mentor told me to do...

help..thanks
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

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

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

  1.  
  2. char temp[100];

how would compare to null...

btw, thank you...
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #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 11:08 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,038
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

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

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

  1. if( *temp_ptr == 0 ) {}
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

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

 
0
  #6
May 11th, 2007
its not working, i just press enter but it does not alert me that i placed nothing... T_T
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,776
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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:
  1. if ( x == "" )
or
  1. 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 here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 112
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

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

 
0
  #9
May 11th, 2007
In c++ you can compare a "null string" by going (x != npos) Cant you?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC