Linked List Questions

I have several problems with my code here:

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const string text = "Save.txt"; //name of text file

class List
{
private:
	class Node
	{
		friend class List;
		int id;
		int phone;
		string descrip;
		string name;
		Node *next;
		Node(int ID, int Phone, string desc, string Nname)
		{
			id = ID;
			phone = Phone;
			descrip = desc;
			name = Nname;
			next = NULL;
		}
		Node()
		{
			id = 0;
			phone = 0;
			descrip = "";
			name = "";
			next = NULL;
		}
	}; 
	Node *head;
public:
	List()
	{
		head = NULL;
	}
	void add(int id, int numb, string des, string name);
	void display(); //displays the list
	void remove(); //removes the list
	~List();
};
void List::add(int id, int numb, string des, string name)
{
	Node *ptr = new Node;
	ptr->id = id;
	ptr->name = name;
	ptr->descrip = des;
	ptr->phone = numb;
	if (head == NULL)
	{
		head = new Node(id, numb, des, name);
	}
	else
	{
		Node *current = head;
		while (current->next != NULL)
		{
			current = current->next;
		}
		current->next = ptr;
		ptr->next = NULL;
	}
	ptr = NULL;
}
void List::remove()
{
	Node *current = head;
	if (head == NULL)
	{
		return;
	}
	else
	{
		while (current)
		{
			Node *garbage = current;
			current = current->next;
			delete garbage;
		}
	}
	current = NULL;
}
void List::display() 
{
	Node *current = head;
	if (head == NULL)
	{
		cout << "Empty list!" << endl;
	}
	else
	{
		while (current->next != NULL)
		{
			cout << current->id << endl;
			cout << current->phone << endl;
			cout << current->descrip << endl;
			cout << current->name << endl;
			current = current->next;
		}
	}
	current = NULL;
}
List::~List()
{
	Node *current = head;
	while (current != NULL)
	{
		Node *garbage = current;
		current = current->next;
		delete garbage;
	}
	current = NULL;
}

void readfile(string file);
void writefile(string file, List *&temp);

int main()
{
	readfile(text);
	
	char choice, prio;
	int id = 0;
	int phone = 0;
	string descrip;
	string buff;
	string name; //temp variables to hold data

	//List Emergency;
	List *Emergency = new List;
	List *Standard = new List;
	do{
		cout << "Select one of the following options by typing the corresponding letter:" << endl;
		cout << "N: Enter a new job\nA: Assign a job\nQ: Quit" << endl;
		cin >> choice;
		if (choice == 'n' || choice == 'N')
		{
			cout << "Job ID: ";
			cin >> id;
			cout << endl;

			cout << "Job Descrption: ";
			getline(cin, descrip); cout << endl;

			cout << "Contact name: ";
			cin >> name;
			cout << endl;

			cout << "Phone ext: " << endl;
			cout << "+"; cin >> phone;

			cout << "Enter 'E' for emergency, or 'S' for standard: " << endl;
			cin >> prio;
			if (prio == 'e' || prio == 'E')
			{
				Emergency->add(id, phone, descrip, name);
			}
			if (prio == 's' || prio == 'S')
			{
				Standard->add(id, phone, descrip, name);
			}
		}
	   if (choice == 'a' || choice == 'A')
		{
			Emergency->display();
			Emergency->remove();
			Standard->display();
			Standard->remove();
		}
	} while (choice != 'q' || choice != 'Q');
	//user has decided to quit
	Emergency->display();
	Standard->display();
	Emergency->remove();
	writefile(text, Standard);

	system("PAUSE");
	return 0;
}
void readfile(string file)
{
	int id;
	string descrip;
	string name;
	int phone;
	ifstream read;
	read.open(file);
	if (read.is_open())
	{
		if (read.eof())
		{
			cout << "There were no saved jobs!" << endl;
			return;
		}
		else
		{
			while (!read.eof()) //while not at end of file
			{
				read >> id;
				read >> descrip;
				read >> name;
				read >> phone;
			}
			read.close();
		}
	}
	else
	{
		cout << "Failed to open the file!" << endl;
	}

}

void writefile(string file, List *&temp)
{
	ofstream f;
	f.open(file);
	if (f.is_open())
	{
		while (!f.eof())
		{
			f << temp;

		}
	}
	temp->remove();
	temp = NULL;

}


1) When I ask the user to enter the job description, I use getline(cin,descrip) since I expect the user to enter a whole list of strings, HOWEVER... what the program outputs is this:
job description:
job name:

it completely skips the option for the user to enter anything in the job description field and heads straight to the next statement: job name:

why?? I tested this out by putting in getline(cin,descrip) outside of the do while loop and it worked fine??

2) My do while loop does not execute when the user enters Q... it keeps on looping over and over again

3) When I add the elements into either list Emergency or Standard, and display the list, it says the list is empty...

1.
1
2
cin >> id; // reads an int, leaves newline into the stream
getline(cin, descrip); // reads the newline 

If you (the user) would not press 'Return' after the integer, then there would be no newline char at the start of the description.

You have to discard the probable newline character after the formatted >> and before doing the unformatted getline().


2. Do you know that line 140 succeeds? You don't test for it. The mismatches of your question 1 will mess things up.


3. Ditto.
Why wouldn't the user not press return? The user has to press return to enter the integer anyway..

"You have to discard the probable newline character after the formatted >> and before doing the unformatted getline()"

This does not make any sense to me, can you please simplify what you are saying


What do you mean if line 140 succeeds? It does succeed since the user does enter N or n to enter in the new data and the loop does go back to asking the menu, so this still doesn't answer my question

Why wouldn't the user not press return? The user has to press return to enter the integer anyway..

"You have to discard the probable newline character after the formatted >> and before doing the unformatted getline()"

What he's saying is that the formatted input extraction for the int is likely to leave a newline in the input stream. The unformatted input extraction done by getline will encounter that newline, and since getline stops at the first newline it encounters, it only extracts an empty string. Mixing formatted and unformatted extraction is a common pitfall for those new to the language.

getline((cin >> std::ws), descrip); will skip any whitespace (including an errant newline) at the beginning of the line.
Last edited on
That's what I thought was the error, I tried multiple things like inputting a lot of cout << endl; to clear the new line

I've never seen getline((cin >> std::ws), descrip); before, it's a shame my class does not cover any of this


I still need help on the remaining questions, thank you!
Bump still need help
Need help with why my linked list won't add those elements

For example, the user enters N to add a new job, the user enters the values, then it asks for the user to enter either E or S for emergency or standard, which are 2 seperate lists, and then it will add to the corresponding list

When I display, the program outputs gibberish, and outputs nothing for the string values
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
void List::add(int id, int numb, string des, string name)
{
	Node *ptr = new Node;
	ptr->id = id;
	ptr->name = name;
	ptr->descrip = des;
	ptr->phone = numb;
	if (head == NULL)
	{
		head = new Node(id, numb, des, name);  //whoops! what about ptr?
	}
	else
	{
		Node *current = head;
		while (current->next != NULL)
		{
			current = current->next;
		}
		current->next = ptr;
		ptr->next = NULL;
	}
	ptr = NULL;
}
Here's your add function. Every time you add a node to an empty list you're leaking memory. How? You don't assign ptr to head, you just create a new node. Now you have head pointing to a new node, and ptr node just kinda hanging out. You will lose any reference to the ptr object when the function ends on line 68.

In your remove function, you should set the head pointer to NULL at the end, not just that current pointer which is local to the function scope.
Last edited on
man so many dumb mistakes... thank you
Topic archived. No new replies allowed.