Click to See Complete Forum and Search --> : reading text from the user


muskrat
October 19th, 2002, 04:38 PM
I would like to know how to read a line of text, that the user inputs, into a string... I can't use cin because it needs to be able to work for more than one word. Thank you for any help!!

Philip Nicoletti
October 19th, 2002, 07:23 PM
use getline() ...


#include <iostream>
#include <string>

using namespace std;

int main()
{
string line;

cout << "Enter a line of input > ";

getline(cin,line);

cout << endl << "user input was > " << line << endl;

return 0;
}


note : if using VC++, depending on the version, you might
need to enter return twice after the user input. DinkumWare
has a fix for this on their web page.

muskrat
October 20th, 2002, 12:17 AM
I have a linux machine and getline is not working on it... I was wondering if getline might be for windows only or something. Thank you very much for any help.

Philip Nicoletti
October 20th, 2002, 03:33 PM
getine() is not Windows specific, so it should
work under linux. I can test that tomorow.

Are you using g++ ? Maybe try my test program
and see if it works. If it works, but your code
doesn't, maybe post your code.