943,502 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 51569
  • C++ RSS
May 1st, 2004
0

Compare strings...

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
wildrider30 is offline Offline
4 posts
since Apr 2004
May 1st, 2004
0

Re: Compare strings...

Hello,

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

C++ Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
May 2nd, 2004
0

Re: Compare strings...

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
Reputation Points: 45
Solved Threads: 0
Light Poster
Bleek is offline Offline
28 posts
since Mar 2004
May 6th, 2004
0

Re: Compare strings...

Use string.h
C++ Syntax (Toggle Plain Text)
  1. strcmp(str1,str2);
returns 0 if they are equal
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
May 6th, 2004
0

Re: Compare strings...

Quote originally posted by FireNet ...
Use string.h
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. if (stringa == stringb) {
  2. cout << "Match" << endl;
  3. } else {
  4. cout << "No match" << endl;
  5. }
HTH
Ben
Reputation Points: 66
Solved Threads: 3
Junior Poster
liliafan is offline Offline
117 posts
since Apr 2004
May 30th, 2010
-1
Re: Compare strings...
#include<iostream>
#include<string>

using namespace std;

void main()
{

string s="hello";
string t="hello";

//std::string;

if(s==t)
cout<<"thek h"<<endl;
else
cout<<"e"<<endl;



}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
billz91 is offline Offline
1 posts
since Apr 2010
Oct 23rd, 2010
0
Re: Compare strings...
You include <string.h> and then string a is used instead of int a , whatever
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stephen Ayodeji is offline Offline
3 posts
since Oct 2010

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: Help with SetTimer() & OnTimer()
Next Thread in C++ Forum Timeline: Assigment operator not working?





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


Follow us on Twitter


© 2011 DaniWeb® LLC