Compare strings...

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

Join Date: Apr 2004
Posts: 4
Reputation: wildrider30 is an unknown quantity at this point 
Solved Threads: 0
wildrider30 wildrider30 is offline Offline
Newbie Poster

Compare strings...

 
0
  #1
May 1st, 2004
Hi there,
Could some1 tell me whats wrong with this code...I know i shudnt b using an int variable but whats the right one for a string...

  1. #include <iostream.h>
  2. void main()
  3. {
  4. int a,b;
  5. cout <<"Enter the first name";
  6. cin >>a;
  7. cout <<"Enter the 2nd no";
  8. cin >>b;
  9. ((a=="Tom") && (b=="Pam")) ? cout<<"Yesss..." : cout<<"Noo";
  10. }
Thanx a million
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Compare strings...

 
0
  #2
May 1st, 2004
Hello,

No, int variables and character / string variables are different types. You want to do something like this:

  1.  
  2. // Have to declare values and [size]
  3. // Note that size must be large enough to handle the
  4. // projected size of the word/sentance *and* the return character
  5. // Overloads will overwrite other data in memory (BAD!)
  6. // Compilers might handle overloads differently.
  7.  
  8. {
  9. char firstname[10], nextname[10];
  10.  
  11. cout << "Enter the firstname: ";
  12. cin >> firstname;
  13.  
  14. }

Now, you also wanted to compare if Firstname == Tom, and Nextname == Pam.

Couple things to remember:

Tom <> tom, Tom <> tOm <> TOm
Pam <> pam

Error checking, my friend!

In your studies, you may have heard of the string processing library. You should use one function to make all the data ALL CAPS or all smalls, and use another function to compare the strings. Here is a hint... strcmp

Good Luck,

Christian
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 28
Reputation: Bleek is an unknown quantity at this point 
Solved Threads: 0
Bleek Bleek is offline Offline
Light Poster

Re: Compare strings...

 
0
  #3
May 2nd, 2004
there is a really helpful library called <algorithm> that can help you with string manipulation and comparing etc. look it up online or a book or something...
good luck
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Compare strings...

 
0
  #4
May 6th, 2004
Use string.h
  1. strcmp(str1,str2);
returns 0 if they are equal
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 113
Reputation: liliafan is on a distinguished road 
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: Compare strings...

 
0
  #5
May 6th, 2004
Originally Posted by FireNet
Use string.h
  1. strcmp(str1,str2);
returns 0 if they are equal
Note this will only work with c strings, if you want to use this function with string you will need to treat them as c style strings ie:
  1. if ((int a = strcmp(stringa.c_str(), stringb.c_str())) == 0) {
  2. cout << "Match" << endl;
  3. } else {
  4. cout << "No match" << endl;
  5. }
With c++ I find it is easier to compare the strings with:
  1. if (stringa == stringb) {
  2. cout << "Match" << endl;
  3. } else {
  4. cout << "No match" << endl;
  5. }
HTH
Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC