I\'m in trouble using std::string::find(). I read strings from console through the following code:
50 while(command.find(exitString) != 0) {
51 std::
You might be expecting that find
returns zero when it found the string, kind of like the way strcmp
works.
But that's not how find
works. find
returns the first index of the found string, which might be zero, or might be something else if the string you're looking for is prepended with spaces, other strings, etc.
If find
doesn't find what you're looking for, it returns string::npos
. So your if...else block should be checking to find if the strings were found or not found, not checking to see if they were at index zero. Like this:
if(command.find(helpString) != string::npos ) {
help();
} else if /// ... etc...
You are reading a line and then calling doSwitch() without checking if its exitString. In that case, when the input is exitString, else block at the end of doSwitch() function is executed, causing the program to print "Command Invalido" before exiting the loop.
Is this what you observed?
If its something else, please let us know for what input your code behaves incorrectly and what is the input and output.