String equality

How do I test if two strings are equal?
1
2
3
4
5
string foo = "whatever";
string bar = "whatever else";

if(foo == bar)
  cout << "they are equal";
Or just use the string comparison strcmp....

1
2
3
4
5
6
7
8
9
10
11
string other;
string that;
if(strcmp(other, "hi")) // comparing the characters in this string with the text possibility "hi."
{
// do whatever you want if they are equal.
}
//Or.
if(strcmp(other, that)) // comparing both strings rather than a string with a possibly extracted text to compare it with alongside another string comparing both strings at once.
{
// do whatever you want if they are equal.
}
Last edited on
No, strcmp() is in the C standard library, it only accepts C strings. And it returns 0 for equality, because it can return two different error codes: http://www.cplusplus.com/reference/clibrary/cstring/strncmp/
Last edited on
No, dude, I said strcmp() not strncmp().

strncmp() is not the same thing. strncmp() is used in C but C++ has strcmp() and strncmp().

I use strcmp() all the time in C++, so don't tell me it's wrong because it isn't(not being mean, sorry. I just hate it when people don't know what they're talking about and accuse me of being wrong).

Could some one else also point out that strcmp() works in C++?

Last edited on
Sorry, I posted the wrong link: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

What I said still holds, though. You certainly don't use it on C++ strings.
Yes, I do.

I use the input/output stream and strcmp() works just fine for me. Maybe it's your compiler?

I use strcmp() to compare the value of two strings with the input/output stream, in Dev-C++, and it works.

I'm not trying to start a fight here, but I'm just letting you know that, at least for me, strcmp() works perfectly fine in C++ that I use, on Dev-C++, compiles fine and yes, they are C++ strings.

Last edited on
Alright spoon licker. Alright. The question has been answered. Nothing to see here.
strcmp() works perfectly fine in C++ that I use

I think you're misunderstanding something. C++ supports two kinds of strings: C strings and C++ strings:

1
2
3
4
5
6
7
8
9
10
#include <string>  // for C++ strings
#include <cstring> // for C strings

using namespace std;

int main()
{
    char c_string[] = "I'm a C string.";
    string cpp_string = "I'm a C++ string.";
}
And it compiles while I use only the input/output stream.

Didn't you understand that?

I don't use C headers for C strings but strcmp() works anyways.

Like I said, maybe it's your compiler?
Last edited on
If your compiler compiles this without an error, it's based on its own extensions:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>  // for C++ strings
#include <cstring> // for C strings
#include <iostream>

using namespace std;

int main()
{
    char c_string[] = "I'm a C string.";
    string cpp_string = "I'm a C++ string.";
    cout << strcmp(cpp_string, c_string) << endl;
}

And Dev C++ is an old, outdated IDE. Get an updated one like Code Blocks or VC++ Express.
Last edited on
Don't feed the troll, people. This is spoon licker. He's obviously lying.
I'm thinking about making a game about trolls =)
Does any compiler in the world complains if you use strcmp in c++ code?I use wxDevC++ IDE and use strcmp all the time without any problem.
What I mean,does it effect portability or is it just not in C++ standard or something else?
you can use strcmp in C++. Just not with std::string.
It doesn't work with std::string , but string has a member function called compare that can take std::string and cstrings. I would use that if you want to compare strings to cstrings, only because I am not sure right now if the == operator is overloaded for cstrings or if it would just promote cstrings to std::strings (well, the standard classes are pretty well written so I'd guess they can take char*, but I am not sure).

Oh and btw: don't talk to spoon licker. He is a troll that comes here every so often. Maybe he'll go away if you ignore him.
Last edited on
closed account (z05DSL3A)
you can use strcmp in C++. Just not with std::string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<string>

using namespace std;

int main()
{
    string string1 = "This is a Sting";
    string string2 = "This is a Sting";
    if (strcmp(string1.c_str(), string2.c_str()) == 0 )
        cout <<"string1 identical to string2" << endl;
    else
        cout <<"string1 NOT identical to string2" << endl;
    return 0;
}


;0)
You didn't use it with std::strings, so point invalid ;D
Um...I'm not a male I'm a female. Why do people accuse me of being a male all of the time?

And of trolling?

I don't troll and I'm not a male....So I'd appreciate it if you actually referred to me as my own gender.
why bother with strcmp? there's a reason the string class was created and it's, at least in part, to make this stuff easier

1
2
3
if(str1 == str2) {
    //code
}

^this accomplished what you want

edit: somehow i missed the very first reply. nothing to see here
Last edited on
Topic archived. No new replies allowed.