Cant get input using getline

Im doing this question here for C++ primer book and im having trouble getting
input from name

6. Put together a program that keeps track of monetary contributions to the Society for the
Preservation of Rightful Influence. It should ask the user to enter the number of contributors
and then solicit the user to enter the name and contribution of each contributor.
The information should be stored in a dynamically allocated array of structures. Each
structure should have two members: a character array (or else a string object) to store
the name and a double member to hold the amount of the contribution. After reading
all the data, the program should display the names and amounts donated for all donors
who contributed $10,000 or more. This list should be headed by the label Grand
Patrons. After that, the program should list the remaining donors. That list should be
headed Patrons. If there are no donors in one of the categories, the program should print
the word “none.” Aside from displaying two categories, the program need do no sorting.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <cstring>

struct patrons
	{
		std::string name[20];
		double contribution[20];
	} grand, normal;


int main(int argc, char** argv) 
{
	const int donation = 10000;
	
	std::string nametemp;
	double contributiontemp;
		int contributor;
	
	

	std::cout << "Enter the number of contributors: ";
	std::cin >> contributor;
	int counter = 0;
	int counter1 = 0;
	int counter2 = 0;
	while(counter < contributor)
	{
		std::cout << "\nContributor #" << counter +1 << "\nEnter name: ";
		std::cin.ignore(donation, '\n');
		std::getline(std::cin,nametemp);
	 	
		std::cout << "\nEnter amount of contribution: ";
		std::cin >> contributiontemp;
		
		if(contributiontemp >= donation)
		{
		
			grand.name[counter2] = nametemp;
			grand.contribution[counter2] = contributiontemp;
			++counter2;	
		}
		else if(contributiontemp < donation)
		{
			
			normal.name[counter1] = nametemp;
			normal.contribution[counter1] = contributiontemp;
			++counter1;
		}
		
		++counter;
	}
	
	std::cout << "\nGrand Patrons\n";
	int counter3 = 0;
	if(counter2 > 0) {
	while(counter3 < counter2)
	{
		std::cout << grand.name[counter3] << " $" << grand.contribution[counter3] << std::endl;
		
		++counter3;
	}
	} 
	else if(counter2 == 0) 
	{ std::cout << "\nNone\n";}
	
	std::cout << "\nPatrons\n";
	int counter4 = 0;
	if(counter1 > 0) {
	while(counter4 < counter1)
	{
		std::cout << normal.name[counter4] << " $" << normal.contribution[counter4] << std::endl;

		++counter4;
	}
	} 
	else if(counter1 == 0) 
	{ std::cout << "\nNone\n";}
	
	
	
	return 0;
}
Last edited on
Add this line before line 30:
1
2
std::cin.ignore( 1000000, '\n' ) ; // **** add this
std::getline(std::cin,nametemp); // before this (original line 30) 

To understan why, see: http://www.cplusplus.com/forum/general/69685/#msg372532
thx it worked
Topic archived. No new replies allowed.