• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Another mind-bending problem...comparing character strings.

I'm trying to input two character strings and, if they are equal, or true, print a statement.  Otherwise- end the program.  Heres the code.

#include <iostream>
using namespace std;

int main()
{
     char stringone[256];
     char stringtwo[256];

     cout << "Enter string one: ";
     cin.get(stringone,256);
     cout << "stringone is: " << stringone << endl;

     cin.ignore(256,'\n');

     cout << "Enter string two: ";
     cin.get(stringtwo,256);
     cout << "String two is: " << stringtwo << endl;
     
     if (stringone==stringtwo)
          cout << "This is the back door";



     return 0;
}

This code acts like these two strings are not true regardless of input.  I dont know if I'm properly comparing them.
Any help- much appreciated.
Rixx

0
rixxagain
Asked:
rixxagain
  • 5
  • 4
  • 3
  • +3
1 Solution
 
cookreCommented:
Here's where things start to get fun (i.e., more complex).

The value of a string reference without a subscript, e.g., 'stringone', is the memory address of that string.

To do the string comparison you want to do, use the 'strcmp' function.  'strcmp(s1,s2)' returns a 0 if the srings are the same, a -1 if s1 is 'less' than s2 (i.e., would appear before s2 in sort order), or a +1 if s1 is greater than s2.

So, the test you want to do would be:

if (0==strcmp(stringone,stringtwo))

Note that 'strcmp' is case sensitive.  To do a case insensitive compare, use 'stricmp'.

0
 
ct.smithCommented:
A minor nitpick to cookre's comment, it's strcmp does not always return -1, 0, or +1, it returns some value less than 0 if s1 is less than s2, zero if they are equal or some value larger than zero if s1 is greater than s2.  You can't count on particular values aside from the zero.
0
 
cookreCommented:
One stands corrected.
(<i>hanging head in shame</i>)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LoungeLizardCommented:
You could of course use the "string" class, which will give you the kind of intuitive "feel" you are looking for i.e. comaprison (==) as well as a lot of other nice features. Of course it does have some overhead (speed and size) but it sure is easy to use.
0
 
rixxagainAuthor Commented:
Thank you gentlemen.  I'll try and get to this today. Tomorrow certainly.  Questions that I do have.
   Strcmp is case sensitive to the contents of the strings?  Like YES does not equal yes?
   Also- what are string classes?  This book that I have really isnt helping me or teaching me much.
Thx again
Rixx
0
 
rixxagainAuthor Commented:
Is there a way to compare an input character string with a predefined character string?  Like input stringone, and compare it to a stringtwo already defined in the program?
0
 
cookreCommented:
That is correct, using strcmp(), "YES" is not equal to "yes" or "YEs" or, yEs", or, well you get the idea.  Using stricmp(), they ARE all equal.


To compare against a fixed string, you can either:

if (0==stricmp(stringone,"wee willie wumpus"))

or:

strcpy(stringtwo,"was a wyly ole wit"); // )strcpy is string copy)
...
if (0==stricmp(stringone,stringtwo))

The latter method has the advantage of letting you use 'stringtwo' numerous places, but, should you want to change the value of it, you need only change it in one place.

As to those string classes, they belong to the much touted 'Object Oriented Programming' (spoken in reverential tones).  There are many arguments for and against the early introduction of such things.  My opinion is that they are best left until later, but to avoid turning the thread into something else, I'll just leave it at that.
0
 
LoungeLizardCommented:
The string class is a class defined in standard C++. Here's a little example of how you use it

#include <string>
#include <iostream>

using namespace std;

void main( )
{
  string s1 = "test1";
  string s2 = "test2";
  string s3 = s1 + " AND " + s2;
  string s4 = "test1 AND test2";
  cout << "The string s1 = " << s1 << endl;
  cout << "The string s2 = " << s2 << endl;
  cout << "The string s3 = " << s3 << endl;
   
  if(s3 == s4)
    cout << "s3 is equal to s4" << endl;
  else
    cout << "s3 is not equal to s4" << endl;    
   
  system("pause");
}
0
 
cookreCommented:
Rixx, go with LL on that.
0
 
ballan00Commented:
If you really like C strings, try this:

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
     char* str1 = "Hello";
     char* str2 = "hello";

     if (strcmp(_strlwr(str1), _strlwr(str2)) == 0)
     {
          cout << "Match!!!" << endl;
     }
     else
     {
          cout << "Don't Match!!!" << endl;
     }

     return 0;
}
0
 
ballan00Commented:
Dummy Me -

My C/C++ is a bit rusty.  The above although it will compare the strings correctly, will also convert them to all lower case.  I am used to toLower functions in other languages that just return a temporary string, and don't actually modify the original.
0
 
manas_ghosalCommented:
stringone and stringtwo are actually the starting memory addresses of two different 256 continuous location which are allocated when you declare within your main function the two statements:
                 char stringone[256];
                 char stringtwo[256];
So since the starting addresses(stringone and stringtwo) of two different array are different so stringone==stringtwo would never be true. To compare the two string you must compare each character of the two string or you can use "strcmp" function, which does the same thing.
best of luck
0
 
rixxagainAuthor Commented:
OK Cookre.  I'll go ahead with LL, and thanks for all your input as well.  You, and all, have been most helpfull. I dont want to hold up this thread any longer, and Im certain that I'll be asking more.  I'm learnin alot more about C++ from this site than that damm book.

Thanks again to all
Rixx
0
 
rixxagainAuthor Commented:
Works like a charm LL.  Its exactly what I wanted to do.  I have to say- I'm really impressed at how well everyone on this site knows C++.
Take care and thanks again.
Rixx
0
 
LoungeLizardCommented:
Always a pleasure. Have fun!
0
 
cookreCommented:
It's always pleasant to see one's beliefs borne out - books and manuals can be great reference material, but you can really learn only by doing.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now