我必须从控制台读取整行并将其存储到std::string
和char
数组中,
e.g。
"Hi this is balaji"
现在我必须阅读上面的字符串并将其存储到string
中。我使用getline()
函数尝试了它。
答案 0 :(得分: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
}
答案 1 :(得分:0)
答案 2 :(得分:0)
也许
string a;
cin >> a;
cout << a << endl;
或类似的东西?