Problem with std::string::find()

2020-05-04 04:41发布

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::cout << "$ ";
 52         getline(std::cin, command);
 53 
 54         doSwitch(command);
 55     }

and then I "switch" on them, through the following function:

 59 void Console::doSwitch(std::string command) {
 60     if(command.find(helpString) == 0) {
 61         help();
 62     } else if(command.find(loadString) == 0) {
 63         try {
 64             doLoad(command);
 65         } catch(std::string str) {
 66             std::cout << str << std::endl;
 67         }
 68     } else if(command.find(dumpProcString) == 0) {
 69         try {
 70             doDumpProc(command);
 71         } catch(std::string str) {
 72             std::cout << str << std::endl;
 73         }
 74     } else if(command.find(dumpMemString) == 0) {
 75         doDumpMem();
 76     } else if(command.find(defmemString) == 0) {
 77         try {
 78             doDefmem(command);
 79         } catch(std::string str) {
 80             std::cout << str << std::endl;
 81         } catch(char *str) {
 82             std::cout << str << std::endl;
 83         }
 84     } else if(command.find(resetString) == 0) {
 85         try {
 86             doReset();
 87         } catch(std::string str) {
 88             std::cout << str << std::endl;
 89         }
 90     } else {
 91         std::cout << "Comando inválido." << std::endl;
 92     }
 93 }

but sometimes it simply doesn't switch correctly. Any clues?

Thanks in advance,

EDIT: I've done some tests and I detected it was falling on the last else-if statement, instead of falling on the last else. Then I checked my code again and found that the root cause was that I forgot to initialize resetString. Problem solved! Thank you everyone.

标签: c++
2条回答
放我归山
2楼-- · 2020-05-04 05:11

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...
查看更多
够拽才男人
3楼-- · 2020-05-04 05:17

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.

查看更多
登录 后发表回答