Problem

Where are my mistakes and what are they? This code is my fisrt code about linked list. So ı cannot found mistakes easily. I would appreciate if you could help.

#include <iostream>
using namespace std;

struct Node{
int id;
string task;
string person;
string date;
Node *next;
};

void add(Node *&);
void search(Node *&);
void remove(Node *&);
void list(Node *&);

int main()
{
char command;
Node *lookedNode = NULL;

cout << "A: Add Task" << endl;
cout << "S: Search for Task" << endl;
cout << "L: List All Tasks" << endl;
cout << "R: Remove Task" << endl;
cout << "E: Exit" << endl;
cout << "Choose an operation: " << endl;
cin >> command;

while(command != 'E'){

switch(command)
{
case 'A':
add(lookedNode);
break;
case 'S':
search(lookedNode);
break;
case 'L':
list(lookedNode);
break;
case 'R':
remove(lookedNode);
break;
case 'E':
return 0;
default:
cout << "Your entry is invalid. Please try again." << endl;
break;
}

cout << "A: Add Task" << endl;
cout << "S: Search for Task" << endl;
cout << "L: List All Tasks" << endl;
cout << "R: Remove Task" << endl;
cout << "E: Exit" << endl;
cout << "Choose an operation: " << endl;
cin >> command;
}

return 0;
}

void add(Node *&firstLooked)
{
string taskName;
string personName;
string time;
Node *current = new Node;
Node *additive = new Node;
current = firstLooked;

cout << "Please enter the task: ";
getline(cin, taskName);
additive->task = taskName;
cout << "Please enter name of person who does the task: ";
getline(cin, personName);
additive->person = personName;
cout << "Please enter the deadline like this: dd.mm.year : ";
getline(cin, time);
additive->date = time;

if(firstLooked == NULL){
firstLooked = additive;
additive->next = firstLooked;
additive->id = 1;
}
else{
while((current->task) > (additive->task)){
current = current->next;
additive->id += 1;
}
additive->id += 1;
current->next = additive;
additive->next = firstLooked;
}

delete current;
cout << endl;
}

void search(Node *&firstLooked)
{
string desired;
Node *current = new Node;
current = firstLooked;

cout << "To search for a task, enter its task name or assigned name: " << endl;
getline(cin, desired);

if(current == NULL){
cout << "List is empty. There is not a node in the list." << endl;
return;
}
else{
if(((firstLooked->task) == desired) || ((firstLooked->person) == desired)){
cout << firstLooked->id << " - " << firstLooked->task << endl;
cout << firstLooked->person << " - " << firstLooked->person << endl;
}
while(current != firstLooked){
current = current->next;
if( ((current->task) == desired) || ((current->person) == desired)){
cout << current->id << " - " << current->task << endl;
cout << current->person << " - " << current->person << endl;
current = firstLooked;
}
}
}
delete current;
}

void remove(Node *&firstlooked)
{
int numberOfNode;
Node *current = new Node;
Node *prev = new Node;
current = firstlooked;

list(firstlooked);
cout << "There is not a node in the list to remove!" << endl;

if(current != NULL){
cout << "Choose the ID of the task you want to remove: ";
cin >> numberOfNode;
while(current != firstlooked){
prev->next = current;
current = current->next;
if(current->id == numberOfNode){
prev->next = current->next;
current = prev->next;
current == firstlooked;
}
}
}
delete current;
delete prev;
}

void list(Node *&firstLooked)
{
Node *current = new Node;
current = firstLooked;

if(current == NULL){
cout << "List is empty!\n";
return;
}
else{
cout << "All tasks are listed below: \n";
current = current->next;
while(current != firstLooked){
cout << current->id << "- " << current->task << endl;
cout << current->person << " - " << current->date << endl;
current = current->next;
}
}
}

This code include lots of error. An output of code:

A: Add Task
S: Search for Task
L: List All Tasks
R: Remove Task
E: Exit
Choose an operation:
A
Please enter the task: Please enter name of person who does the task: John
Please enter the deadline like this: dd.mm.year : 12.03.2014

A: Add Task
S: Search for Task
L: List All Tasks
R: Remove Task
E: Exit
Choose an operation:
A
Please enter the task: Please enter name of person who does the task: William
Please enter the deadline like this: dd.mm.year : 14.02.2014

A: Add Task
S: Search for Task
L: List All Tasks
R: Remove Task
E: Exit
Choose an operation:
L
All tasks are listed below:

Process returned -1073741819 (0xC0000005) execution time : 41.936 s
Press any key to continue.
this does not even compile on my machine.
therefore your compiler will tell you where your mistakes are.
I've taken a quick look at your code and run it thru a memory checker.

A few comments.
1. You should use the format tags when posting code. I've posted it with indentation so I could read it. But infuture, you should not post unformatted code.

2. You need to go thru the logic one operation at a time, and check the linked list is correct. As you're using Windows, I expect you have easy access to a good debugger.

There's just too much code and too many obvious failures (like call remove with an unrecognized id). You really need to learn how to fix this yourself.

You formatted code:
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
#include <iostream>
using namespace std;

struct Node{
	int id;
	string task;
	string person;
	string date;
	Node *next;
};

void add(Node *&);
void search(Node *&);
void remove(Node *&);
void list(Node *&);

int main()
{
	char command;
	Node *lookedNode = NULL;

	cout << "A: Add Task" << endl;
	cout << "S: Search for Task" << endl;
	cout << "L: List All Tasks" << endl;
	cout << "R: Remove Task" << endl;
	cout << "E: Exit" << endl;
	cout << "Choose an operation: " << endl;
	cin >> command;

	while(command != 'E'){

		switch(command)
		{
		case 'A':
			add(lookedNode);
			break;
		case 'S':
			search(lookedNode);
			break;
		case 'L':
			list(lookedNode);
			break;
		case 'R':
			remove(lookedNode);
			break;
		case 'E':
			return 0;
		default:
			cout << "Your entry is invalid. Please try again." << endl;
			break;
		}

		cout << "A: Add Task" << endl;
		cout << "S: Search for Task" << endl;
		cout << "L: List All Tasks" << endl;
		cout << "R: Remove Task" << endl;
		cout << "E: Exit" << endl;
		cout << "Choose an operation: " << endl;
		cin >> command;
	}

	return 0;
}

void add(Node *&firstLooked)
{
	string taskName;
	string personName;
	string time;
	Node *current = new Node;
	Node *additive = new Node;
	current = firstLooked;

	cout << "Please enter the task: ";
	getline(cin, taskName);
	additive->task = taskName;
	cout << "Please enter name of person who does the task: ";
	getline(cin, personName);
	additive->person = personName;
	cout << "Please enter the deadline like this: dd.mm.year : ";
	getline(cin, time);
	additive->date = time;

	if(firstLooked == NULL){
		firstLooked = additive;
		additive->next = firstLooked;
		additive->id = 1;
	}
	else{
		while((current->task) > (additive->task)){
			current = current->next;
			additive->id += 1;
		}
		additive->id += 1;
		current->next = additive;
		additive->next = firstLooked;
	}

	delete current;
	cout << endl;
}

void search(Node *&firstLooked)
{
	string desired;
	Node *current = new Node;
	current = firstLooked;

	cout << "To search for a task, enter its task name or assigned name: " << endl;
	getline(cin, desired);

	if(current == NULL){
		cout << "List is empty. There is not a node in the list." << endl;
		return;
	}
	else{
		if(((firstLooked->task) == desired) || ((firstLooked->person) == desired)){
			cout << firstLooked->id << " - " << firstLooked->task << endl;
			cout << firstLooked->person << " - " << firstLooked->person << endl;
		}
		while(current != firstLooked){
			current = current->next;
			if( ((current->task) == desired) || ((current->person) == desired)){
				cout << current->id << " - " << current->task << endl;
				cout << current->person << " - " << current->person << endl;
				current = firstLooked;
			}
		}
	}
	delete current;
}

void remove(Node *&firstlooked)
{
	int numberOfNode;
	Node *current = new Node;
	Node *prev = new Node;
	current = firstlooked;

	list(firstlooked);
	cout << "There is not a node in the list to remove!" << endl;

	if(current != NULL){
		cout << "Choose the ID of the task you want to remove: ";
		cin >> numberOfNode;
		while(current != firstlooked){
			prev->next = current;
			current = current->next;
			if(current->id == numberOfNode){
				prev->next = current->next;
				current = prev->next;
				current == firstlooked;
			}
		}
	}
	delete current;
	delete prev;
}

void list(Node *&firstLooked)
{
	Node *current = new Node;
	current = firstLooked;

	if(current == NULL){
		cout << "List is empty!\n";
		return;
	}
	else{
		cout << "All tasks are listed below: \n";
		current = current->next;
		while(current != firstLooked){
			cout << current->id << "- " << current->task << endl;
			cout << current->person << " - " << current->date << endl;
			current = current->next;
		}
	}
}
mutexe (866) >> in my complier, there is not a problem during compiling. I wish there was a problem during compiling, so maybe ı can understand what my mistakes are; but there is not a problem during compiling. Thank you.
kbw (6676) >> ı take into account your comments. I hope, i can learn how to fix this myself. Thank you.
Topic archived. No new replies allowed.