C++ 无限循环,返回问题

C++ Infinite-loop, Issue with returning

本文关键字:问题 返回 无限循环 C++      更新时间:2023-10-16
void phonebookmenu() {
    phonebook ph;
    string str;
    cin.ignore();
    cout << "PH> ";
    getline(cin, str); //Input from user.
    string buf; // Have a buffer string.
    stringstream ss(str); // Insert the string into a stream.
    vector<string> tokens; // Create vector to hold the words.
    while (ss >> buf){
        tokens.push_back(buf); //Adds all the words inside the vector.
    }
    while (true){
    if (tokens[0] == "add"){
            ph.add(tokens[1],tokens[2]);
    }
    else if(tokens[0] == "lookup"){
            ph.lookup(tokens[1]);
    }
    else if(tokens[0] == "change"){
            ph.change(tokens[1],tokens[2]);
    }
    else if(tokens[0] == "alias"){
            ph.alias(tokens[1],tokens[2]);
    }
    else if(tokens[0] == "quit"){
            //Return to the "Main-menu"
    }
    else{
        cout << "Invalid input" << endl;
    }
}

所以我从"主菜单"调用此菜单,但在我输入"添加 peter 123"之类的内容后,它会执行该功能,然后返回到我不想要的"主菜单"。它应该回到

cout << "PH> ";  

所以我可以继续做手术。

循环不围绕cout/input/determine/do something逻辑。它只在determine/do something附近.将while移到cout之前,看看会发生什么。请务必将最有帮助的答案标记为答案。