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.
}
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++?
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.
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
usingnamespace std;
int main()
{
char c_string[] = "I'm a C string.";
string cpp_string = "I'm a C++ string.";
}
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>
usingnamespace 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.
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?
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.