如何从控制台读取行并将其存储到c ++中的字符串中?

bal*_*aji 6 c++ linux

我必须从控制台读取整行并将其存储到一个std::string和一个char数组中,例如

"Hi this is balaji"
Run Code Online (Sandbox Code Playgroud)

现在我必须阅读上面的字符串并将其存储到string.我尝试使用该getline()功能.

Mar*_*ork 17

尝试:

#include <string>
#include <iostream>

int main()
{
    std::string line;

    std::getline(std::cin, line);  // read a line from std::cin into line

    std::cout << "Your Line Was (" << line << ")\n";

    std::getline(std::cin, line);  // Waits for the user to hit enter before closing the program
}
Run Code Online (Sandbox Code Playgroud)

  • @balaji:不要试试. (2认同)