簡體   English   中英

一個std :: endl生成三個std :: endl(s)?

[英]One std::endl makes three std::endl(s)?

我想為一個較大的程序編寫一個小的命令行解釋器示例。 但是,如果我輸入“ 1 2 3”,輸出將是“ 1 \\ n2 \\ n3 \\ n”,而不是我期望的“ 1 2 3 \\ n”。

#include <iostream>

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

    return 0;
}

您應該嘗試使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM