problem with cin.getline(name) and cin.get(name)

Hi!

I'm trying to work on a question in c++ primer.
Unfortunately, the program doesn't work correctly when i use cin.getline(individual[i].name, SIZE) instead of cin >> individual[i].name which seems to work perfectly. I want to read a line for a name, any assistance is very much appreciated!

My program looks like this:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>

using namespace std;

int main ()
{
    const int SIZE = 20;
    int n;
    int i;
    int count;
	
    struct info {
        char name[SIZE];
	double amount;
    };
	
    cout << "How many names?\n";
    cin >> n;
	
    info * individual = new info [n];
	
    i = 0;
    while (i < n)
        {
	cout << "Enter name";
	cin.getline(individual[i].name, SIZE); // doesn't work
        cout << "Enter amount";
	cin >> individual[i].amount;
	i++;
	}
	
	cout << "\nGrand Patrons:\n\n";
	i = 0;
	count = 0;
	while (i < n)
	{
	if (individual[i].amount >= 10000)
	{
	cout << individual[i].name << endl;
	count++;
	}
	i++;
	}
	
	if (count == 0)
		cout << "None\n";
	
	cout << "\nPatrons:\n\n";
	i = 0;
	count = 0;
	while (i < n)
	{
	if (individual[i].amount < 10000)
	{
	cout << individual[i].name << endl;
	count++;
	}
        i++;
	}	
	
	if (count == 0)
	    cout << "None\n";
	
	delete [] individual;
	
}


Thanks in advance!

Cheers!
Unicyclist
Last edited on
If you mix integer input and string input, make you sure you clean up stdin after each integer input.

 
    while(cin.get() != '\n');


this is, because cin >> integer leaves a '\n' in stdin. So if you try to read a name, the program think, you typed a empty line and then proceed to amount input and so on.
Hi!

Thanks for the quick reply. Point noted...
However, I still seem to have a problem with the line

 
cin.getline(individual[i].name, SIZE);


i don't even have an opportunity to enter an integer... the program just skips to the end
ie:

1
2
cout << "Enter amount";
cin >> individual[i].amount;


doesn't get a chance to run.

Cheers!
Unicyclist
Last edited on
The problem is that you type first an integer. How many names? Integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	cout << "How many names?\n";
	cin >> n;
	while (cin.get() != '\n'); // remove remaining trash

	info * individual = new info [n];

	i = 0;
	while (i < n)
	{
		cout << "Enter name";
		cin.getline(individual[i].name, SIZE); // doesn't work
		cout << "Enter amount";
		cin >> individual[i].amount;
		while (cin.get() != '\n'); // remove remaining trash
		i++;
	}


[
lol. I forgot all about the cin >> n;!

Thanks for the help! it works perfectly now!

Cheers!
Unicyclist
Be careful with mixing cin >>, getline and get. get does not clear the delimiter character (a newline) out of the input buffer after you press enter (it doesn't, right?), so getline will take that newline as input and skip over the user's entry.
Topic archived. No new replies allowed.