populate function is not working properly!

Hello,

I am not finished with the program yet but the populate function isn't working properly. The first record is only taking the phone number, but the second and the third records are taking both name and the phone number. can anyone tell me what is wrong?
Thank you

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include<iostream>
#include<String>
#include<iomanip>
using namespace std;

const int numofels=3;

struct teletype
{
	string name;
	string phoneno;
	teletype *nextAdd;
};

void populate(teletype *);
void display(teletype *);
void insert(teletype *);
void modify(teletype *);
void remove(teletype *);



int main()
{
	int choice;
	teletype *list, *current, *newrecpoint;

	list = new teletype;
	current = list;
	
	while (true)
	{
		{
			cout << "Pick an option from the following menu:" << endl;
			cout << "1. Create an initial list of name and phone numbers" << endl;
			cout << "2. Insert a new structure into the linked list" << endl;
			cout << "3. Modify an existing structure in the linked list" << endl;
			cout << "4. Delete an existing structure from the list" << endl;
			cout << "5. Exit from the program" << endl << endl;

			cin >> choice;
		}

		if (choice == 1){
			
			for (int i = 0; i < numofels-1 ; i++)
				{
					populate(current);
					current->nextAdd = new teletype;
					current = current->nextAdd;
				}
				populate(current);
				current->nextAdd = NULL;
				cout << "The list is:" << endl;
				display(list);
				}
		else if (choice == 2){
			{
				newrecpoint = new teletype;
				insert(newrecpoint);
			}	
		}
		

		else if (choice == 5){
			cout << "Good Bye!";
			exit(0);
		}
		else
		cout << "Enter a number 1-5 " << endl<<endl;
	}



	system("pause");
	return 0;
}

void populate(teletype *record)
{
	
	cout << "Enter a name: ";
	getline(cin, record->name);
	cout << "Enter the phone number: ";
	getline(cin, record->phoneno);
	cout << endl;
	return;
}

void display(teletype *contents)  
{
	while (contents != NULL)
	{
		cout << endl << setiosflags(ios::left)
			<< setw(30) << contents->name
			<< setw(20) << contents->phoneno;
		contents = contents->nextAdd;
	}
	cout << endl;
	return;
}

void insert(teletype *newrecord)
{
	cout << "Enter a name: ";
	getline(cin, newrecord->name);
	cout << "Enter the phone number: ";
	getline(cin, newrecord->phoneno);
	cout << endl;
	return;
}
The function is working fine. The issue is that formatted input operations (such as line 41) leave the newline in the stream buffer. The next time you call getline, it sees that newline character and gets a blank line from it.

If you want a quick fix, add this after line 41:
http://www.cplusplus.com/forum/beginner/1988/
(the cin.ignore line)
Topic archived. No new replies allowed.