getline ( ) & get ( ) - Programmers Heaven

Howdy, Stranger!

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

Categories

getline ( ) & get ( )

johnk85johnk85 Posts: 63Member
Having a problem with my getline & get program. It compiles with no errors. I get the output requesting a sentence, and type in the sentence and hit enter but nothing; can't figure it out.
Here is what I have:

#include
#include

using std::cout;
using std::endl;
using std::cin;
using std::ios;

const int SIZE = 80;

int main()
{
char array[ SIZE ], array2[ SIZE ], c;
cout << "Enter a sentence to test getline() and get():
";
cin.getline( array, SIZE, '*' );
cout << array << '
';
cin >> c; // read next character in input
cout << "The next character in the input is: " << c << '
';
cin.get( array2, SIZE, '*' );
cout << array2 << '
';
cin >> c; // read next character in input
cout << "The next character in the input is: " << c << '
';
return 0;
}

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [blue]Its because you told getline that the line terminator was an asterisk.[/blue]
    [code]
    [red]using std::endl;[/red]
    [red]using endl has nothing to do with your problem, but using endl instead of "
    " is a more generic way to end a line[/red]

    const int SIZE = 80;

    int main()
    {
    char array[ SIZE ], array2[ SIZE ], c;
    cout << "Enter a sentence to test getline() and get():" << endl;
    [red]cin.getline( array, SIZE); [/red]
    cout << array << endl;
    cin >> c; // read next character in input
    cout << "The next character in the input is: " << c << endl;
    cin.get( array2, SIZE);
    cout << array2 << endl;
    cin >> c; // read next character in input
    cout << "The next character in the input is: " << c << endl;
    return 0;
    }
    [/code]

    : Having a problem with my getline & get program. It compiles with no errors. I get the output requesting a sentence, and type in the sentence and hit enter but nothing; can't figure it out.
    : Here is what I have:
    :
    : #include <iostream>
    : #include
    :
    : using std::cout;
    : using std::endl;
    : using std::cin;
    : using std::ios;
    :
    : const int SIZE = 80;
    :
    : int main()
    : {
    : char array[ SIZE ], array2[ SIZE ], c;
    : cout << "Enter a sentence to test getline() and get():
    ";
    : cin.getline( array, SIZE, '*' );
    : cout << array << '
    ';
    : cin >> c; // read next character in input
    : cout << "The next character in the input is: " << c << '
    ';
    : cin.get( array2, SIZE, '*' );
    : cout << array2 << '
    ';
    : cin >> c; // read next character in input
    : cout << "The next character in the input is: " << c << '
    ';
    : return 0;
    : }
    :

  • johnk85johnk85 Posts: 63Member
    : [blue]Its because you told getline that the line terminator was an asterisk.[/blue]
    : [code]
    : [red]using std::endl;[/red]
    : [red]using endl has nothing to do with your problem, but using endl instead of "
    " is a more generic way to end a line[/red]
    :
    : const int SIZE = 80;
    :
    : int main()
    : {
    : char array[ SIZE ], array2[ SIZE ], c;
    : cout << "Enter a sentence to test getline() and get():" << endl;
    : [red]cin.getline( array, SIZE); [/red]
    : cout << array << endl;
    : cin >> c; // read next character in input
    : cout << "The next character in the input is: " << c << endl;
    : cin.get( array2, SIZE);
    : cout << array2 << endl;
    : cin >> c; // read next character in input
    : cout << "The next character in the input is: " << c << endl;
    : return 0;
    : }
    : [/code]
    :
    : : Having a problem with my getline & get program. It compiles with no errors. I get the output requesting a sentence, and type in the sentence and hit enter but nothing; can't figure it out.
    : : Here is what I have:
    : :
    : : #include <iostream>
    : : #include
    : :
    : : using std::cout;
    : : using std::endl;
    : : using std::cin;
    : : using std::ios;
    : :
    : : const int SIZE = 80;
    : :
    : : int main()
    : : {
    : : char array[ SIZE ], array2[ SIZE ], c;
    : : cout << "Enter a sentence to test getline() and get():
    ";
    : : cin.getline( array, SIZE, '*' );
    : : cout << array << '
    ';
    : : cin >> c; // read next character in input
    : : cout << "The next character in the input is: " << c << '
    ';
    : : cin.get( array2, SIZE, '*' );
    : : cout << array2 << '
    ';
    : : cin >> c; // read next character in input
    : : cout << "The next character in the input is: " << c << '
    ';
    : : return 0;
    : : }
    : :
    :
    :
    Going back through the question in my book the above program doesn't really give me what i think i need. So i've redone the program, but so you understand what i'm looking for here is the question:

    Write a program to show that the getline and three-argument get istream member functions each end the input string with a string-terminating null character. Also, show that get leaves the delimiter character on the input stream while getline extracts the delimiter character and discards it.

    Now the only problem that i think i have is that getline part isn't working. The cin>> displays 'The' Get displays all but 'The' and the getline displays nothing.

    Here is what i have now:

    #include

    using std::cout;
    using std::cin;
    using std::endl;

    int main()
    {
    const int SIZE = 80;
    char buffer[ SIZE ], buffer1[ SIZE ], buffer2[ SIZE ];

    cout << "Enter a sentence to test get() & getline():

    ";
    cin >> buffer;
    cout << "
    The string read with cin is:
    "
    << buffer << "

    ";

    cin.get( buffer1, SIZE );
    cout << "The string read with cin.get is:
    "
    << buffer1 << "

    ";

    cin.getline( buffer2, SIZE );
    cout << "The string read with cin.getline is:
    "
    << buffer2 << endl;



    return 0;
    }
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2003-4-24 20:14:40[/red][/b][hr]
    [blue]See [red]RED[/red] below. There may be another way to do this but I don't know what it is.[/blue]
    : Going back through the question in my book the above program doesn't really give me what i think i need. So i've redone the program, but so you understand what i'm looking for here is the question:
    :
    : Write a program to show that the getline and three-argument get istream member functions each end the input string with a string-terminating null character. Also, show that get leaves the delimiter character on the input stream while getline extracts the delimiter character and discards it.
    :
    : Now the only problem that i think i have is that getline part isn't working. The cin>> displays 'The' Get displays all but 'The' and the getline displays nothing.
    :
    : Here is what i have now:
    :
    : #include
    :
    : using std::cout;
    : using std::cin;
    : using std::endl;
    :
    : int main()
    : {
    : const int SIZE = 80;
    : char buffer[ SIZE ], buffer1[ SIZE ], buffer2[ SIZE ];
    :
    : cout << "Enter a sentence to test get() & getline():

    ";
    : cin >> buffer;
    : cout << "
    The string read with cin is:
    "
    : << buffer << "

    ";
    :
    [red]fflush(stdin)[/red]
    : cin.get( buffer1, SIZE );
    : cout << "The string read with cin.get is:
    "
    : << buffer1 << "

    ";
    :
    [red]fflush(stdin)[/red]
    : cin.getline( buffer2, SIZE );
    : cout << "The string read with cin.getline is:
    "
    : << buffer2 << endl;
    :
    :
    :
    : return 0;
    : }
    :



  • johnk85johnk85 Posts: 63Member
    : [b][red]This message was edited by stober at 2003-4-24 20:14:40[/red][/b][hr]
    : [blue]See [red]RED[/red] below. There may be another way to do this but I don't know what it is.[/blue]
    : : Going back through the question in my book the above program doesn't really give me what i think i need. So i've redone the program, but so you understand what i'm looking for here is the question:
    : :
    : : Write a program to show that the getline and three-argument get istream member functions each end the input string with a string-terminating null character. Also, show that get leaves the delimiter character on the input stream while getline extracts the delimiter character and discards it.
    : :
    : : Now the only problem that i think i have is that getline part isn't working. The cin>> displays 'The' Get displays all but 'The' and the getline displays nothing.
    : :
    : : Here is what i have now:
    : :
    : : #include
    : :
    : : using std::cout;
    : : using std::cin;
    : : using std::endl;
    : :
    : : int main()
    : : {
    : : const int SIZE = 80;
    : : char buffer[ SIZE ], buffer1[ SIZE ], buffer2[ SIZE ];
    : :
    : : cout << "Enter a sentence to test get() & getline():

    ";
    : : cin >> buffer;
    : : cout << "
    The string read with cin is:
    "
    : : << buffer << "

    ";
    : :
    : [red]fflush(stdin)[/red]
    : : cin.get( buffer1, SIZE );
    : : cout << "The string read with cin.get is:
    "
    : : << buffer1 << "

    ";
    : :
    : [red]fflush(stdin)[/red]
    : : cin.getline( buffer2, SIZE );
    : : cout << "The string read with cin.getline is:
    "
    : : << buffer2 << endl;
    : :
    : :
    : :
    : : return 0;
    : : }
    : :
    :
    :
    :
    :
    Thanks Stober!
Sign In or Register to comment.