Difference cin.get and getline

Hey guys,

I was wondering, what's the difference between cin.get and the getline function?
They're both functions to get an inputted string right?

Thanks in advance! :)
cin.get gets one char only
cin.getline gets a c-string
getline gets an std::string
ok, thanks!
Yeah I kinda get confused about when to use these functions as well :(.

But you can use cin.get() like this to get a line of text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    cout << "Please enter a line of text" << endl;
    char char1[20];
    cin.get(char1, 20);
    
    cout << "Char: " << char1;

    cin.get(); //pause
    cin.get();
    return 0;
}

Oh. Right.
cin.get ( char* s, streamsize n, char delim ); and cin.getline (char* s, streamsize n, char delim ); is almost the same thing. The first one doesn't discard delim from input stream however.

The best thing would be to see a reference:
http://www.cplusplus.com/reference/iostream/istream/get/
http://www.cplusplus.com/reference/iostream/istream/getline/
http://www.cplusplus.com/reference/string/getline/
Topic archived. No new replies allowed.