使用最多3个命令读取输入命令

时间:2014-07-30 21:43:00

标签: c++ eclipse command cin

我有一个程序,提示用户输入命令。根据该命令,它将执行不同的功能。

示例命令:

help
set value 20
set another 30
print this
print that

如何将这些分成3个独立的变量并继续执行?我遇到了一个简单的

问题
string command;
string item;
string value;
cin >> command >> item >> value;

因为除非用户输入全部三个,否则程序将不会继续。

这就是我想出的,我无法回答我自己的问题,所以这样做。

感谢您的输入。这是我在研究了@JoachimPileborg提到的一些功能后想出的。它似乎工作正常我需要它。

int main() {

    bool done = false;
    char input[30];
    std::string command, item, value;

    //output instructions, get user input
    std::cout << "Type 'help' to find more about commands.\n";
    do{

    std::cin.getline(input,30);

    //parse user input from char to an array
    istringstream iss;
    string commands[3];
    iss.str (input);
    for (int i = 0; i < 3; i++) {
        iss >> commands[i];
    }

    //set each part of the array to appropriate value
    command = commands[0];
    item = commands[1];
    value = commands[2];

    //properly sort out which command is being called
    //first: check if command has 3 parts
    if (commands[2].length() != 0){
        cout << command << item << value;
    }
    //second:if it doesnt have 3 parts, check if it has 2 parts
    else if (commands[1].length() != 0){
        cout << command << item;
    }
    //third:if it doesn't have 2 parts, check for 1 part
    else if (commands[0].length() != 0){
        if (command == "help"){
            commandline.help();
            done = true;
        }
        else{
        cout << "Incorrect Command! Please try again!";
        }
    }
    else
        cout << "No command found, please try again!";
    }while(!done);
}

1 个答案:

答案 0 :(得分:0)

  

&#34;如何将这些变量分成3个单独的变量并继续执行程序?&#34;

不要有不同的变量,使用适当的标准库容器来收集为命令提供的可变数量的参数。

最简单的方法, - 我可以想象 - ,如何实现这样的事情,可能看起来像这样:

std::string cmdline;

while(cmdline != "quit") {
    std::cout << "Enter command > ";
    std::getline(std::cin),cmdline);
    if(!cmdline.empty()) {
        std::istringstream iss(cmdline);
        std::vector<std::string> cmditems;
        std::string cmditem;
        while(iss >> cmditem) {
            cmditems.push_back(cmditem);
        }
        if(cmditems.empty()) { // Just whitespaces input
            continue; 
        }
        // accessing cmditems[0] is always safe at this point
        if(cmditems[0] == "help") {
            printHelp();
        }
        else if(cmditems[0] == "set") {
            // Check if the number of command arguments actually 
            // fit the required ones
            if(cmditems.size() < 3) { 
                std::cerr << "Please enter a name and value!" << std::endl;
                continue;
            }
            setValue(cmditems[1],cmditems[2]);
        }
        else if(cmditems[0] == "print") {
            // Check if the number of command arguments actually 
            // fit the required ones
            if(cmditems.size() < 2) {
                std::cerr << "Please enter a name to print!" << std::endl;
                continue;
            }
            printValue(cmditems[1]);
        }
        // and so on ...
    }        
}