Why can I not fully alter data members of a dynamically added structure?

Hello,

I am using an insert function to add a node to a doubly linked list after the tail node. When I call a populate function to fill the data members of the structures, I can only input 1 for one member of the structure and not the other.

[output]
The list will now be made
Enter a name:
1
Enter the phone number: 1
Enter a name:
2
Enter the phone number: 2
Enter a name:
3
Enter the phone number: 3

The list consists of the following records:

1 1 00000000 00E63370
2 2 00E632F0 00E63330
3 3 00E63370 00000000
Which record would you like to remove?
select 1 if you want to remove the head, 2 for the middle and 3 for the tail, 4 to insert a new record
4

Enter a name:
Enter the phone number: 5

1 1 00000000 00E63370
2 2 00E632F0 00E63330
3 3 00E63370 009842C8
5 00E63330 00000000
[\output]


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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct TeleType
{
	string name;
	string phoneNo;
	TeleType *nextaddr;
	TeleType *prevaddr;
};

void populate(TeleType *); // function prototype needed by main()
void display(TeleType *); // function prototype needed by main()
void remove_head(TeleType *); //function prototype for main()
void remove_current(TeleType *); //function prototype for main()
void remove_tail(TeleType *); //function prototype for main()
void make_list();
void insert(TeleType *);

TeleType head, tail, current;

TeleType *phead = &head;
TeleType *pcurrent = &current;
TeleType *ptail = &tail; // 3 pointers to structures of
						 // type TeleType
						 // get a pointer to the first structure in the list

int main()
{
	int i;
	cout << "The list will now be made" << endl;

	make_list();

	cout << "\nThe list consists of the following records:\n";

	display(phead); // display the structures

	cout << "Which record would you like to remove?" << endl;
	cout << "select 1 if you want to remove the head, 2 for the middle and 3 for the tail, 4 to insert a new record" << endl;

	cin >> i;

	cout << endl;

	switch (i)
	{
	case 1:
		remove_head(phead);
		display(pcurrent);
		break;
	case 2:
		remove_current(pcurrent);
		display(phead);
		break;
	case 3:
		remove_tail(pcurrent);
		display(phead);
		break;
	case 4:
		insert(ptail);
		display(phead);
		break;
	}

	system("pause");

	return 0;
}

void make_list()
{
	//Input values for head structure
	populate(phead);
	//Assign nextaddr pointer to current
	head.nextaddr = pcurrent;
	//Input values for current structure
	populate(pcurrent);
	//Assign current structure's newaddr and preaddr
	current.prevaddr = phead;
	current.nextaddr = ptail;
	//Input values for tail
	populate(ptail);
	//Assign prevaddr pointer
	tail.prevaddr = pcurrent;
	//assign newaddr pointer as null to tail structure
	tail.nextaddr = NULL;
	// assign prevaddr pointer as current pointer  for tail structure
	return;
}

// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType

	cout << "Enter a name: " << endl;
	getline(cin, record->name);

	cout << "Enter the phone number: ";
	getline(cin, record->phoneNo);

	return;
}

void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType

	while (contents != NULL) // display until end of linked list
	{
		cout << endl << setiosflags(ios::left)
			<< setw(30) << contents->name
			<< setw(20) << contents->phoneNo
			<< setw(20) << contents->prevaddr
			<< setw(20) << contents->nextaddr;
		contents = contents->nextaddr;
	}
	cout << endl;

	return;
}

void remove_head(TeleType *record)
{
	current.prevaddr = NULL;
	current.nextaddr = ptail;

	tail.prevaddr = pcurrent;
	tail.nextaddr = NULL;

	return;
}

void remove_current(TeleType *record)
{
	head.nextaddr = ptail;

	tail.prevaddr = phead;
	tail.nextaddr = NULL;

	return;
}

void remove_tail(TeleType *record)
{
	current.nextaddr = NULL;
	current.prevaddr = phead;

	return;
}

void insert(TeleType *record)
{
	TeleType *ptr_new_record = new TeleType; //create a new structure, return an address and assign it to a ptr
												//new record.

	record->nextaddr = ptr_new_record; //change the nextaddr pointer to point to the new record

	ptr_new_record->nextaddr = NULL; //assign new null pointer
	ptr_new_record->prevaddr = ptail; //assign new pointer of new record to point to the the old tail structure.

	populate(ptr_new_record); //call populate function to fill in data members of the structure.

	return;
}
1
2
3
4
5
	cout << "Which record would you like to remove?" << endl;
	cout << "select 1 if you want to remove the head, 2 for the middle and 3 for the tail, 4 to insert a new record" << endl;

	cin >> i;
	cin.ignore(); //<--- add this 
you pressed <Return> when you input your choice, so the input buffer contains "4\n", `i' gets the 4, but the "\n" remains in the input buffer, so when you do getline(cin, record->name); in `populate()' the name will have an empty string.
cin.ignore() will discard that line break.
Your linked list handling code doesn't look right to me. Normally head and tail are just pointers to the first and last items in the list. The items are allocated dynamically. You have allocated global nodes for the head and tail. So what should happen if you try to delete the head?

Why do you have a "current" node? I understand the need for a current pointer, but not a current node. The "current" node will be different ones depending on what you're doing.

These problems bleed into remove_head(), remove_current() and remove_tail(). None of them do anything with the record parameter.

My advice is to think about what these functions are supposed to do. Write it down as a comment before the function and the make the code do what the function is supposed to do.
Hello,

I have since tried your recommendations. They've helped me to make some more leeway!
Topic archived. No new replies allowed.