-7

I am just messing around in C++ with some things I recently learned and I wanted to know how to correctly compare two strings to each other. I looked at a previous thread for help, but I am not sure I am getting the variables right and there was a repeating error. (P.S. This is executed to the command prompt.)

    string Users = "Username1";
    //Set an empty string.
    string UserChoice; 

    //Print out a line that warns the user to type a user.
    std::cout << "Username: "; 
    std::cin >> UserChoice;

    //If the user types out whatever "Users" is, run the code below.
    if (strcmp(Users, UserChoice) == 0){

    //Do Stuff

    }

3 Answers3

7

You want:

 if (Users == UserChoice) {

The std::string class (well, really std::basic_string) overloads the == operator (and many others) to do what you want. You should not be using C functions like strcmp in C++ code, and in any case they cannot be directly applied to C++ std::strings.

  • Is there even a way that I can compare strings then? What if it wasn't actually a string variable and something else? Sorry, I am almost completely new to this. – user7437922 Jan 19 '17 at 00:17
  • 2
    @user The code I posted compares strings. You need to read a good book on C++ (and don't ask "which book") . You are not going to learn the basics of C++ by posting questions on SO. –  Jan 19 '17 at 00:22
  • Note: equality is case sensitive, e.g. 'n' != 'N'. To compare for case insensitive, *transform* the string to all lower case or all upper case before comparing. – Thomas Matthews Jan 19 '17 at 00:26
  • Basically C++ doesn't have strings, then the standard library is written to make it look as though it has. An std:;string is a wrapper over a vector (array) or characters. Using the == operator causes library code to be executed. Building your own string class is a nice little exercise. – Malcolm McLean Jan 19 '17 at 00:27
  • @Malcolm Basically, it does have strings, in the same way it has vectors. The C++ Standard Library is an integral part of the C++ Language, not some optional extra. –  Jan 19 '17 at 00:30
  • Okay cool. @late please don't get salty with people, its not taken as nice and actually kind of rude. I already have lots of resources on C++, I just haven't dove into them yet. I have worked with a different language prior that was a lot easier when it came to matters like this. – user7437922 Jan 19 '17 at 00:31
  • @user " I have worked with a different language prior that was a lot easier when it came to matters like this" easier than using the pretty much standard == comparison operator? And " please don't get salty with people, its not taken as nice and actually kind of rude." - do not tell me how to use Stack Overflow. –  Jan 19 '17 at 00:34
  • @late I tried doing so before, and for some reason I was unsuccessful. In fact it said that I could NOT use operators on strings in the error, that is why I tried a different method. Tone it down man, be nice. – user7437922 Jan 19 '17 at 00:36
  • 2
    @user7437922 Somehow I fail to see _any_ salt in his statements. In my opinion, it is you, who is overreacting. _I tried doing so before, and for some reason I was unsuccessful_ And, why do you think, that your attempt, was unsuccessful? Compilation error? Runtime exception? Unexpected output? If unexpected output, were the strings _really_ equal? – Algirdas Preidžius Jan 19 '17 at 00:48
  • "and don't ask "which book") . You are not going to learn the basics of C++ by posting questions on SO.", "easier than using the pretty much standard == comparison operator?","do not tell me how to use Stack Overflow" all sounded kind of angry to me, its not important and I really don't have patience discussing this thread any further. – user7437922 Jan 19 '17 at 00:55
0

Comparing strings is the same as comparing int values, char values, etc... . You should use the following method:

    string a
    string b
    if (a == b)
    {
        // Do something
    }

In your case, 'a' and 'b' would be replaced by 'Users', 'UserChoices'. But the basic format of comparing 2 variables of the same type stays the same regardless of the type (I'm not sure whether there are any exceptions to this rule or not).

It is also recommended, just as @latedeveloper mentioned, not to use c-language functions in a c++ program. The 2 languages are NOT interchangeable!

** Helpful tip: Always strive to keep your code as simple as possible. With some exceptions possible, the more complicated you make your code, the more hard you will make it for others to understand your code. To connect it to your case, why use a function strcmp() when you can keep it simple by using the == sign? This is just my 2 bits based on personal experience.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
-2

c style:

string a 
string b 
if(strcmp(a.c_str(),b.c.str()) == 0)
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
maple0607
  • 1
  • 1