954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Compare strings...

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...

#include <iostream.h>
  void main()
  {
  int a,b;
  cout <<"Enter the first name";
  cin >>a;
  cout <<"Enter the 2nd no";
  cin >>b;
  ((a=="Tom") && (b=="Pam")) ? cout<<"Yesss..." : cout<<"Noo";
  }

Thanx a million

wildrider30
Newbie Poster
4 posts since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

Hello,

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

// Have to declare values and [size]
// Note that size must be large enough to handle the
// projected size of the word/sentance *and* the return character
// Overloads will overwrite other data in memory (BAD!)
// Compilers might handle overloads differently.

{
    char firstname[10], nextname[10];

    cout << "Enter the firstname: ";
    cin >> firstname;

}


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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

there is a really helpful library called that can help you with string manipulation and comparing etc. look it up online or a book or something...
good luck

Bleek
Light Poster
28 posts since Mar 2004
Reputation Points: 45
Solved Threads: 0
 

Use string.h

strcmp(str1,str2);

returns 0 if they are equal

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

Use string.h

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:

if ((int a = strcmp(stringa.c_str(), stringb.c_str())) == 0) {
   cout << "Match" << endl;
} else {
   cout << "No match" << endl;
}

With c++ I find it is easier to compare the strings with:

if (stringa == stringb) {
   cout << "Match" << endl;
} else {
   cout << "No match" << endl;
}

HTH
Ben

liliafan
Junior Poster
117 posts since Apr 2004
Reputation Points: 66
Solved Threads: 3
 

#include
#include

using namespace std;

void main()
{

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

//std::string;

if(s==t)
cout<<"thek h"<

billz91
Newbie Poster
1 post since Apr 2010
Reputation Points: 9
Solved Threads: 0
 

You include and then string a is used instead of int a , whatever

DubyStev
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You