从字符串中获取所有单词到多个字符串c ++

时间:2014-04-28 03:19:33

标签: c++

我正在尝试接受输入命令(例如设定速率1)并将这三个单词分成2个字符串和整数。当我运行此代码时,我只收到第一个单词(集)。我做错了什么?。

void Tool::toolInterface(){
    string input;
    string partInput;
    string partInput1;
    int partInput2;

    //string delimiter = " ";
    cout << "auDiskTool, version 1.0.0. Type ‘help’ to find more about commands\n";
    cout << ">";

    cin >> input;

    int length = input.length();
    char str[length];
    string buf;
    stringstream ss(input);
    vector<string> indInput;

    while(!ss.eof()) {
        ss >> partInput;
        ss >> partInput1;
        ss >> partInput2
    }

    cout << partInput;
    cout << partInput1;
    cout << partInput2;    
}

2 个答案:

答案 0 :(得分:2)

cin >> input;  will read 1 word and store it in input. 

如果要读取多个输入数据,请使用相同数量的变量,如下所示。

http://ideone.com/RSKO3t

答案 1 :(得分:1)

cin遇到空格时会停止阅读。

尝试使用:

getline(cin,input);
相关问题