Cpp equal operator not working as expected | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

25th Sep 2018, 10:26 AM
CodeMStr
CodeMStr - avatar
2 Answers
+ 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
25th Sep 2018, 10:49 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
Kinshuk Vasisht thanks for the reply
25th Sep 2018, 10:54 AM
CodeMStr
CodeMStr - avatar