hi,i got 2 variables of string format.how to compare if they are equal????
it gives me error.Code:string a="Asd"; string b="Asd"; if(strcmp(a,b)==0){ cout<<"true"; }
hi,i got 2 variables of string format.how to compare if they are equal????
it gives me error.Code:string a="Asd"; string b="Asd"; if(strcmp(a,b)==0){ cout<<"true"; }
> if(strcmp(a,b)==0){
It's even easier.
Code:if (a == b){
A long(er) way (using the string container's compare member function):
Another long way (using the strcmp function):Code:string a="Asd"; string b="Asd"; if( a.compare(b) == 0 ) { cout<<"true"; }
But yeah, just using operator== is short and sweet.Code:string a="Asd"; string b="Asd"; if(strcmp(a.c_str(),b.c_str())==0){ cout<<"true"; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
strcmp() is only for c-style strings. like char*string_thing. the class string is a C++ construct and will not work with functions that require a char*.
>> the class string is a C++ construct and will not work with functions that require a char*.
Technically it can work if you use the c_str() member function, but in most cases there are better alternatives that are specific to the C++ string class.