getline function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

getline function

jedi06jedi06 Posts: 38Member
In the getline function you have to specify how many characters you want to extract. [blue]istream& getline (char* s, streamsize n, char delim );[/blue]

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?

Comments

  • jambeardjambeard Posts: 70Member
    [b][red]This message was edited by jambeard at 2006-4-2 14:28:9[/red][/b][hr]
    : In the getline function you have to specify how many characters you want to extract. [blue]istream& getline (char* s, streamsize n, char delim );[/blue]
    :
    : 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:

    [code]
    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!='
    ' )
    {
    s = s + c; // add the string
    c = in.get(); // read the next character
    }

    returnString = s;

    return returnString;
    }
    [/code]

    And use it like this:

    [code]
    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;
    }
    [/code]

    Hope it helps.

    J


  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by jambeard at 2006-4-2 14:28:9[/red][/b][hr]
    : : In the getline function you have to specify how many characters you want to extract. [blue]istream& getline (char* s, streamsize n, char delim );[/blue]
    : :
    : : 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:
    :
    : [code]
    : 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!='
    ' )
    : {
    : s = s + c; // add the string
    : c = in.get(); // read the next character
    : }
    :
    : returnString = s;
    :
    : return returnString;
    : }
    : [/code]
    :
    : And use it like this:
    :
    : [code]
    : 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;
    : }
    : [/code]
    :
    : Hope it helps.
    :
    : J
    :
    :
    :


    And for C, replace cin.get() with fgets().
  • Federal101Federal101 Posts: 36Member
    : : [b][red]This message was edited by jambeard at 2006-4-2 14:28:9[/red][/b][hr]
    : : : In the getline function you have to specify how many characters you want to extract. [blue]istream& getline (char* s, streamsize n, char delim );[/blue]
    : : :
    : : : 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:
    : :
    : : [code]
    : : 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!='
    ' )
    : : {
    : : s = s + c; // add the string
    : : c = in.get(); // read the next character
    : : }
    : :
    : : returnString = s;
    : :
    : : return returnString;
    : : }
    : : [/code]
    : :
    : : And use it like this:
    : :
    : : [code]
    : : 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;
    : : }
    : : [/code]
    : :
    : : Hope it helps.
    : :
    : : J
    : :
    : :
    : :
    :
    :
    : And for C, replace cin.get() with fgets().
    :
    Or you could just use..
    [code]
    #include <string>
    #include
    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;
    }
    [/code]
  • jambeardjambeard Posts: 70Member

    : Or you could just use..
    : [code]
    : #include
    : #include
    : 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;
    : }
    : [/code]
    :

    You could but wasn't there a bug in that (or a version of it)? I tried using it for a project only 3 months ago and had to give up and write the getline function myself.
  • Federal101Federal101 Posts: 36Member
    :
    : : Or you could just use..
    : : [code]
    : : #include
    : : #include
    : : 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;
    : : }
    : : [/code]
    : :
    :
    : You could but wasn't there a bug in that (or a version of it)? I tried using it for a project only 3 months ago and had to give up and write the getline function myself.

    VS 6.0 has the famous getline bug.

    Here is the page that shows the fix

    http://support.microsoft.com/default.aspx?scid=kb;en-us;240015

    Once you make this change getline should work for you.
Sign In or Register to comment.