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"; }
This is a discussion on compare if 2 string types are equal within the C++ Programming forums, part of the General Programming Boards category; hi,i got 2 variables of string format.how to compare if they are equal???? 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"; }
![]()
I used to be an adventurer like you... then I took an arrow to the knee.
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.