开发者

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

开发者 https://www.devze.com 2023-03-13 22:12 出处:网络
Please see the code fragment below, specifically the first \'else if\' statement.I want the user to have the ability to do this:

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

精彩评论

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