+ 1
Cpp equal operator not working as expected
so I was messing around with Cpp and I wanted to ask for user input and if the statement is the same as I want, then it would run a function. but it doesn't work as expected, can anyone help me? [code following] https://code.sololearn.com/cz5UzA7iBE5d/?ref=app
2 Réponses
+ 5
The == operator should not be used on C-Strings (char pointers), as the == operator compares their addresses and not the contiguous values.
What you should use is std::string objects instead of char* and then the overloaded == operator will work just as required. You will also be able to perform a lot more operations later.
For more information, visit :
www.cplusplus.com/reference/string/string
en.cppreference.com/w/cpp/header/string
Also, if you don't want to use std::string, then compare using strcmp from <cstring>.
www.cplusplus.com/reference/cstring/strcmp
+ 1
Kinshuk Vasisht thanks for the reply