2017-07-02 57 views
0

我想爲一個更大的程序編寫一個小的命令行解釋器示例。但是如果我輸入「1 2 3」,輸出爲「1 \ n2 \ n3 \ n」而不是「1 2 3 \ n」,正如我所期望的那樣。一個std :: endl使三個std :: endl(s)?

#include <iostream> 

int main(int argc, char **argv) { 
    while (true) { 
     std::string line; 
     std::cin >> line; 
     std::cout << line << std::endl; 
    } 

    return 0; 
} 

回答

1

你應該嘗試getline函數。 getline將提供您的預期輸出

#include <iostream> 

int main(int argc, char **argv) { 
    while (true) { 
     std::string line; 
     std::getline (std::cin, line); 
     std::cout << line << std::endl; 
    } 

    return 0; 
} 
+0

啊OK cin自動讀取,如果有幾個單詞。 –

相關問題