: :
This message was edited by jambeard at 2006-4-2 14:28:9
: : : In the getline function you have to specify how many characters you want to extract.
istream& getline (char* s, streamsize n, char delim );
: : :
: : : To me that is dumb, becuase it is not going to actually get the line if it is larger than you expected. Is there a way to get the whole line no matter how large it is?
: : :
: :
: :
: : If I were you I'd sack off trying to use that, I found using the getline function loads of hassle!!
: :
: : Try this:
: :
: :
: : string readLine(istream &in, string &s)
: : {
: : string returnString;
: :
: : char c;
: :
: : s.erase(); // clear the old string
: :
: : c = in.get(); // read one character ahead
: : while ( c!='\n' )
: : {
: : s = s + c; // add the string
: : c = in.get(); // read the next character
: : }
: :
: : returnString = s;
: :
: : return returnString;
: : }
: :
: :
: : And use it like this:
: :
: :
: : int main()
: : {
: : string lineOfText; //Place to store the line of text
: :
: : readLine(cin, lineOfText); //read the line
: :
: : cout << "You wrote " << lineOfText << endl; //Output what the user entered
: :
: : return 0;
: : }
: :
: :
: : Hope it helps.
: :
: : J
: :
: :
: :
:
:
: And for C, replace cin.get() with fgets().
:
Or you could just use..
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
string input;
cout << "Enter a string: ";
getline( cin, input );
cout << "The string is: " << input << endl;
return 0;
}