简体   繁体   English

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

[英]C++ Infinite-loop, Issue with returning

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;
    }
}

So i'm calling this menu from a "Main-menu" but after i have input something like "Add peter 123" it does the function then returns to the "Main-menu" which i don't want.因此,我从“主菜单”调用此菜单,但在我输入“添加彼得 123”之类的内容后,它会执行该功能,然后返回到我不想要的“主菜单”。 It's supposed to go back to它应该回到

cout << "PH> ";  

So i can continue to make operations.所以我可以继续做手术。

The loop is not around the cout/input/determine/do something logic.循环不是围绕cout/input/determine/do something逻辑。 It's only around the determine/do something .它只是围绕着determine/do something Move the while to before the cout and see what happens.while移到cout之前,看看会发生什么。 Be sure to mark the answer that helps most as an answer.请务必将最有帮助的答案标记为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM