C ++中的Quit-option和while循环

时间:2018-05-08 13:31:25

标签: c++ c++11

在我的方法中,我希望能够键入一行字符串,它会将第一个单词作为命令过滤掉,将其他单词作为参数过滤掉。每一行都以" $"开始。

这应该是一个连续的循环,直到我输入" CTR_C"。如果我输入" CTR_C"我应该被问到是否要戒烟。如果" y"如果" n"我将退出该方法。应该有" $"再次,我可以继续输入我的字符串。

现在在这一部分,当我输入" n"时,我不会回到我的while(myLoop)-loop而我被踢出了该方法。我忽略了什么错误?

int read_command(char *command, char *parameters[]) { // prompt for user input and read a command line 

// getline, extract command and parameters, set noParam, ...
// ...

int noParam = 0;
bool myLoop{true};
string myCommand{};
vector<string> paramVec{};
vector<string> words;

string line;
while (myLoop) {

    cout << "$ ";


    while (getline(cin, line)) {
        int test{};
        istringstream iss(line);
        string word;
        unsigned i = 0;

        while (iss >> word) {

            if (word != "CTR_C") {
                words.push_back(word);
                ++i;
                test++;
            } else {

                string yn{};

                cout << "Do you want to quit (y/n)?" << endl;

                cin >> yn;
                if (yn == "y") {
                    myLoop = false;
                    break;

                } else {
                    if (yn == "n") {
                        cout << "nicht abbrechen" << endl;
                        myLoop = true;
                        test = 999;


                    } else {
                        cout << "Eingabe ungueltig" << endl;
                        myLoop = true;
                        test = 999;

                    }
                }

            }

        }

        if (test == 0) {
            myLoop = false;
            break;
        } else {
            if (test == 999) {

                cout << "try again" << endl;
                myLoop = true;

            } else {

                //extract command
                myCommand = words.at(0);
                cout << "Command is: " << myCommand << endl;

                //extract parameters                
                for (int i = 1; i < words.size(); i++) {
                    paramVec.push_back(words.at(i));
                }
                cout << "Param is: ";
                for (int i = 0; i < paramVec.size(); i++) {
                    cout << paramVec.at(i) << endl;
                }

            }
        }

        break;

    }

}

return (noParam);

};

1 个答案:

答案 0 :(得分:0)

你必须在&#34; test == 999&#34;之前添加这两行。指示

            cin.clear();
            cin.ignore(10000, '\n');

您的代码必须如下所示:

            if (test == 999) {
            cout << "try again" << endl;
            myLoop = true;
            cin.clear();
            cin.ignore(10000, '\n');
        }

现在可以使用,如果有这样的声明,还可以使用其他一个提示:

        if (test == 0) {
            //INSTRUCTION1
        }
        else if (test == 999) {
            //INSTRUCTION2
        }
        else {
            //INSTRUCTION3
        }

不喜欢你:

    if (test == 0) {
      //INSTRUCTION1
        }
        else {
            if (test == 999) {
         //INSTRUCTION2
            }
            else {
        //INSTRUCTION3
            }
        }