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

C++ 使用最多3个命令读取输入命令,c++,eclipse,command,cin,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; 因为除非用户输入所有三个,否则程序将无法继续 这

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

命令示例:

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);
}
intmain(){
bool done=false;
字符输入[30];
std::string命令、项、值;
//输出指令,获取用户输入
std::cout>命令[i];
}
//将数组的每个部分设置为适当的值
命令=命令[0];
项目=命令[1];
值=命令[2];
//正确地排序正在调用的命令
//第一:检查命令是否有3个部分
if(命令[2].length()!=0){
库特
“如何将这些变量拆分为3个单独的变量并继续程序?”

如果没有不同的变量,请使用适当的标准库容器来收集为命令给定的可变数量的参数

最简单的方法,我可以想象,如何实现这样的事情,可能是这样的:

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 ...
    }        
}
std::string cmdline;
while(cmdline!=“退出”){
std::cout>cmditem){
cmditems.push_back(cmditem);
}
if(cmditems.empty()){//仅输入空格
继续;
}
//此时访问cmditems[0]始终是安全的
如果(cmditems[0]=“帮助”){
打印帮助();
}
else if(cmditems[0]=“设置”){
//检查命令参数的数量是否实际
//符合要求的
如果(cmditems.size()<3){

在读取命令行之类的内容时,不要使用输入运算符
>
。相反,我建议您使用,然后使用,并将单独的字符串放入。您可以将整行读取为字符串,并根据空格分隔符拆分字符串。然后根据字符串拆分为的单词数,从中继续e、 为了进一步帮助您,请将命令名和存储在中,然后很容易找到要调用特定命令的函数。将包含命令行的向量作为参数传递给函数,类似于将
argv
传递给
main
函数的方式。@Ben如果有空格,如何将整行读入字符串?@Jo我对C++很陌生,我对这些功能不太熟悉,我会研究一下。你能提供一些例子吗?