1

コマンドラインから入力として行を取得しようとしています。私の問題は、行全体が取得されていないことですが、スペースによってトークン化されています。

だから、「数学が大好き」などと入力すると、

"you enterend: I like Math a lot"

私は次のようになります:

EDITING MODE: Enter a command
i like Math a lot
you entered i

EDITING MODE: Enter a command
you entered like

EDITING MODE: Enter a command
you entered Math

EDITING MODE: Enter a command
you entered a

EDITING MODE: Enter a command
you entered lot


void enterEditingMode(){
    editingMode = TRUE;
    static string CMD = "\nEDITING MODE: Enter a command\n";
    string input;
    while(editingMode == TRUE){
        cout << CMD;
        cin >> input;
        //we assume input is always correct
        // here we need to parse the instruction
        cout << "you entered " << input <<endl;
4

2 に答える 2

12

std::getline一度に1行の入力を読み取る標準的な方法です。

次のように使用できます。

std::getline(std::cin, string);

これは、への暗黙的な変換を持つ入力ストリームへの参照を返すvoid*ため、次のように簡単に成功を確認できます。

if (std::getline(std::cin, string))
{
    // successfully read a line...
}
于 2009-11-15T00:16:23.257 に答える
1

cin.getline(input);

詳細については、 http://www.cplusplus.com/reference/iostream/istream/getline/を参照してください。

于 2009-11-15T00:16:58.523 に答える