comparison between string literal

char arrays or char pointers aren’t really the same thing as string class objects in C++, so this

if (option == "foo")

Doesn’t compare the string option to the string literal “foo” it compares the address of option with the address of the string literal “foo”. You need to use one of the many string comparison functions if you want to know if the option is the same as “foo”. strcmp is the obvious way to do this, or you can use std::string instead of char*

Leave a Comment