944,059 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 34693
  • C++ RSS
May 10th, 2007
0

how to compare a string to null...

Expand Post »
hello there, im new in c++...i just want to know how to compare a string to null like:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 10th, 2007
0

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

string class has no overloaded "==" operator. You can do

      string temp;
      cin >> temp;
 
      if(temp.empty())
            cout << "null";
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
May 10th, 2007
0

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

uhmm, what about the;

C++ Syntax (Toggle Plain Text)
  1.  
  2. char temp[100];

how would compare to null...

btw, thank you...
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 10th, 2007
0

Re: how to compare a string to 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 11:08 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 11th, 2007
0

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

Does it work in C++ like this?:

C++ Syntax (Toggle Plain Text)
  1. if( *temp_ptr == 0 ) {}
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 11th, 2007
0

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

its not working, i just press enter but it does not alert me that i placed nothing... T_T
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
May 11th, 2007
0

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

>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:
C++ Syntax (Toggle Plain Text)
  1. if ( x == "" )
or
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 11th, 2007
0

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

> i just press enter but it does not alert me that i placed nothing.

Maybe the newline is getting the best of you...
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 11th, 2007
0

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

In c++ you can compare a "null string" by going (x != npos) Cant you?
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
May 11th, 2007
0

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

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: String class
Next Thread in C++ Forum Timeline: Reasonable or not





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC