开发者

Simple question: Why does the string library function std::string::find always return std::string::npos?

Please see the code fragment below, specifically the first 'else if' statement. I want the user to have the ability to do this:

load filename

so I want to check that "load " is in the string and attempt to open whatever is after "load ". However, string::npos seems to always be returned (string::npos just means there is no position).

I'm probably doing something stupid - this is basic stuff!

void Main::user_choice() {
    string choice;
    while(choice != "exit") {
        cout << "> ";
        cin >> choice;
        if(choice == "view") {
            Main::view_frameworks();
        }
        else if(choice.find("load ") != string::npos) {

        }
        else 开发者_如何学Goif(choice == "exit") {
            return;
        }
        else {
            cout << "Invalid command" << endl;
        }
    }
}


cin >> choice stops reading before the first whitespace. You want getline(std::cin, choice) here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜