开发者

how to read line from console and store it into string in c++?

I have to read a whole line from the console and store it into a std::string and a char array, e.g.

"Hi this is balaji"

Now I开发者_C百科 have to read the above string and store it into string. I tried it using the getline() function.


Try:

#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
}


Maybe there's something wrong with how you use cin.getline()?

  cin.getline (name,256);

C++ refence for getline()


Maybe

string a;
cin >> a;

cout << a << endl;

Or something like that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜