Difference between get() and getline()

So I'm currently teaching myself C++ from a primer book I have and have run into a tricky part of the book I simply don't understand.

The book's example of the program runs just fine. But when I change up the code a little bit just to test my programming abilities, the program closes before calling the function for a second time if I use the get() function. However if I use getline() it works just fine. Can someone please explain what is happening here?

This is the book's code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  // using the delete function to manage memory
#include <iostream>
#include <cstring>
using namespace std;
char* getname();

int main()
{
	char* name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;

	cin.get();
	return 0;
}

char* getname()
{
	char temp[80];
	cout << "Enter last name: ";
	cin >> temp;
	char* pn = new char[strlen(temp) + 1];
	strcpy(pn, temp);

	return pn;
}


This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// using the delete function to manage memory
#include <iostream>
#include <cstring>
using namespace std;
char* getname();

int main()
{
	char* name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;

	cin.get();
	return 0;
}

char* getname()
{
	char temp[80];
	cout << "Enter last name: ";
	cin.getline(temp, 79); // replace getline() with get() and the program doesn't work properly... Why?
	char* pn = new char[strlen(temp) + 1];
	strcpy(pn, temp);

	return pn;
}
I understand that get() only gets one character. But the way that I use it in this program:
cin.get(temp, 79);
should get the whole line, correct?
So I figured out a fix to this, somehow an extra newline character is getting placed into the input stream. So what I don't understand now is shouldn't that newline character be placed into name when it calls getname() a second time? Therefore the cin.get() right before the program terminates should pause the program right? What is the flaw to my logic?
Basically, the difference between get and getline when you're reading C strings is that get leaves the delimiting character (a newline character, in this case) in the input buffer as the next character to be read, while getline actually extracts (and discards) the delimiting character.

So the reason the code doesn't work when you use get is that after the first time you call cin.get(temp, 79);, it leaves the delimiting newline character in the input buffer.
Then, the second time you call it, cin immediately runs into the delimiting character (which is still in the input buffer) and thinks that the input stops right there, so it doesn't read any characters.

It works with getline since getline gets rid of that delimiter after it reads the input.

Just for fun, try using get, but stick a cin.ignore(); call after it:
27
28
29
30
cin.get(temp, 79);
cin.ignore(); // Discard one character from the input buffer (the newline)
char* pn = new char[strlen(temp) + 1];
// ... 

800!
Last edited on
so the second call doesn't remove the newline character from the buffer? That would make sense. Ok thank you very much!
The second call doesn't remove the newline character because it stops as soon as it hits a newline character (which just so happens to be the first character, since the first call to cin.get left it there).
Topic archived. No new replies allowed.