Howdy, Stranger!

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

Categories

C++ getline()

lsvteclsvtec Member Posts: 6
[b][red]This message was edited by lsvtec at 2003-1-4 20:2:52[/red][/b][hr]
I am new to c++ and dont really understand the advantage of using getline().If you dont mind could you explain it to me.

thanks


Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : [b][red]This message was edited by lsvtec at 2003-1-4 20:2:52[/red][/b][hr]
    : I am new to c++ and dont really understand the advantage of using getline().If you dont mind could you explain it to me.
    :
    : thanks
    :
    :
    :


    you use getline() to read an entire line of text form a file or stdin (user input at the keyboard). The line is normally terminated when the CR/LF (windows) or CR (unix) is encountered.
  • DariusDarius Member Posts: 1,666
    : : [b][red]This message was edited by lsvtec at 2003-1-4 20:2:52[/red][/b][hr]
    : : I am new to c++ and dont really understand the advantage of using getline().If you dont mind could you explain it to me.
    : :
    : : thanks
    : :
    : :
    : :
    :
    :
    : you use getline() to read an entire line of text form a file or stdin (user input at the keyboard). The line is normally terminated when the CR/LF (windows) or CR (unix) is encountered.
    :
    Actually CRLF ("
    ") is Windows, LF ('
    ') is Unix (the One True Endline Character) and CR ('
    ') is Mac. This is basically the main difference between opening a file in text mode and opening a file in "binary" mode (in text mode these are automatically converted to/from '
    ').

    As to the original question: "understand the advantage of getline()" relative what? Or did you just mean what is getline() though it seems kind of obvious to me... maybe I'm just on the ball today.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • lsvteclsvtec Member Posts: 6
    : : : [b][red]This message was edited by lsvtec at 2003-1-4 20:2:52[/red][/b][hr]
    : : : I am new to c++ and dont really understand the advantage of using getline().If you dont mind could you explain it to me.
    : : :
    : : : thanks
    : : :
    : : :
    : : :
    : :
    : :
    : : you use getline() to read an entire line of text form a file or stdin (user input at the keyboard). The line is normally terminated when the CR/LF (windows) or CR (unix) is encountered.
    : :
    : Actually CRLF ("
    ") is Windows, LF ('
    ') is Unix (the One True Endline Character) and CR ('
    ') is Mac. This is basically the main difference between opening a file in text mode and opening a file in "binary" mode (in text mode these are automatically converted to/from '
    ').
    :
    : As to the original question: "understand the advantage of getline()" relative what? Or did you just mean what is getline() though it seems kind of obvious to me... maybe I'm just on the ball today.
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :
    :
    :
    :
    :
    :So by using the cin.getline(chararray, num); the advantage is that it can pull up large amount of text?





  • lsvteclsvtec Member Posts: 6
    : [b][red]This message was edited by lsvtec at 2003-1-4 20:2:52[/red][/b][hr]
    : I am new to c++ and dont really understand the advantage of using getline().If you dont mind could you explain it to me.
    :
    : thanks
    :
    :
    :
    :This is the question that i have to answer.
    Write a program that prompts the user for his or her full name and weight.Store these values in variables and print them to the screen.(Use getline() for the name).Also what is the advatage of using getline() rather than cin when getting strings of user input?

    I am doing a home course so i have no teacher to ask so all the help you can give me would be appreciated.
  • stoberstober Member Posts: 9,765 ✭✭✭
    It is often neccessary to write a small example program to demonstrate to yourself how some function(s) works. In the case of getline, you could easily see the difference between cin.getline and cin >> somevariable.

    Here is the demo program
    [code]
    #include
    #include
    using namespace std;

    int main()
    {
    string response;
    char buffer[255];
    cout << "Enter some words: ";
    cin.getline(buffer, sizeof(buffer));
    cout << "Enter some more words: ";
    cin >> response;
    cout << buffer << endl << response << endl;
    cin.ignore();
    return 0;
    }
    [/code]




    This is the output. Note that cin.getline() allows you to enter a bunch of words with embedded spaces, while the other forms of user input do not.

    [code]
    Enter some words: one two three
    Enter some more words: four five six
    one two three
    four
    Press any key to continue
    [/code]

  • lsvteclsvtec Member Posts: 6
    : It is often neccessary to write a small example program to demonstrate to yourself how some function(s) works. In the case of getline, you could easily see the difference between cin.getline and cin >> somevariable.
    :
    : Here is the demo program
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : string response;
    : char buffer[255];
    : cout << "Enter some words: ";
    : cin.getline(buffer, sizeof(buffer));
    : cout << "Enter some more words: ";
    : cin >> response;
    : cout << buffer << endl << response << endl;
    : cin.ignore();
    : return 0;
    : }
    : [/code]
    :
    :
    :
    :
    : This is the output. Note that cin.getline() allows you to enter a bunch of words with embedded spaces, while the other forms of user input do not.
    :
    : [code]
    : Enter some words: one two three
    : Enter some more words: four five six
    : one two three
    : four
    : Press any key to continue
    : [/code]
    :
    :
    :
    :
    :
    :
    :
    :
    :Thank you that helped me out alot.The example helped a lot.
    thanks
Sign In or Register to comment.