Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

linux

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 have to read the above string and store it into string. I tried it using the getline() function.

like image 319
balaji Avatar asked Apr 22 '11 20:04

balaji


1 Answers

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
}
like image 183
Martin York Avatar answered Nov 09 '22 22:11

Martin York