Helppp with linked list in C++

Hey guys,
i need help with my code. The program specification is to create a phone directory using linked list. i've done everything i know but the program stops working after the user inputs the telephone number. I originally worked on this on my desktop and i'm trying to run it on my pc because i'm not not home so i really don't know if my pc is just acting up. Can someone please try to run this code and test it to see if it works properly or if i made a mistake.

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
 #include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

struct node *FIRST = NULL;
struct node *LAST = NULL;

struct node
{
	string f_name;
	string l_name;
	string no;
	struct node *next;
};
void Display()
{
	int i=1;
	struct node *ptr = FIRST;
	if(FIRST==NULL)
	{
		cout<<"List is empty....!!!\n";
	}
	else
	{
		while(ptr != NULL) 
		{
			cout<<"\nPerson :- "<<i<<"\n\tPerson's First Name :- "<<ptr->f_name<<"\n\tPerson's Last Name :- "<<ptr->l_name<<"\n\tPerson's phone number :- "<<ptr->no;
			i++;
			ptr = ptr->next;
	   }	
	}
}
void insert(string f_name,string l_name,string no)
{
	struct node *link = (struct node*) malloc(sizeof(struct node));
	link->f_name = f_name;
	link->l_name = l_name;
	link->no=no;
	if(FIRST==NULL)
	{
		FIRST=link;
		LAST=link;
		LAST->next=NULL;
	}
	else
	{
		link->next=FIRST;
		FIRST=link;
	}
}
void Edit(string key)
{
	string fname,lname;
	string no;
	int flag=1;
	struct node* current = FIRST;
	if(FIRST == NULL) 
	{		
		flag=0;
	}
	if (flag!=0)
	{
		while(current->f_name != key) 
		{	
			if(current->next == NULL) 
			{
				cout<<"\n\tCan't find the First Name of Person from the list....\n";
				flag=0;
				break;
			}
			else 
			{       
				current = current->next;
			}
		}
	}      
	if (flag!=0)
	{
		cout<<"\tEnter the NEW FIRST NAME of Person  :-";
		cin>>fname;				
		cout<<"\tEnter the NEW LAST NAME of Person :-";
		cin>>lname;
		cout<<"\tEnter the NEW PHONE NUMBER of Person :-";
		cin>>no;
		current->no=no;		
		current->f_name=fname;
		current->l_name=lname;
	}	

}
void append(string f_name,string l_name,string no)
{
	struct node *link = (struct node*) malloc(sizeof(struct node));
	link->f_name = f_name;
	link->l_name = l_name;
	link->no=no;

	if(FIRST==NULL)
	{
		FIRST=link;
		LAST=link;
		LAST->next=NULL;
	}
	else
	{
		LAST->next=link;
		LAST=link;
		LAST->next=NULL;		
	}
}
void Deletef()
{
	if(FIRST!=NULL)
	{
		FIRST=FIRST->next;	
	}	
}
void Deletel()
{
	struct node *ptr = FIRST;
	if(FIRST!=NULL)
	{
		while(ptr->next != LAST) 
		{
			ptr = ptr->next;
		}
		ptr->next=NULL;
		LAST=ptr;		
	}
}
int main()
{
	int choice,i;
	string fname,lname,key;
	string no;
	do
	{
		cout<<"\n1.Create a link.\n2.Display all Created links\n3.Edit one link\n4.Delete link\n100.Exit\nChoice:-";
		cin>>choice;
		switch(choice)
		{
			default:
				cout<<"Enter proper choice...!!";
				break;
			case 100:
				break;
			case 1:		
				cout<<"\t1.Append a link\n\t2.Attach at First\n\tChoice :- ";
				cin>>i;		
				cout<<"\tEnter the FIRST NAME of Person :-";
				cin>>fname;				
				cout<<"\tEnter the LAST NAME of Person :-";
				cin>>lname;
				cout<<"\tEnter the NEW PHONE NUMBER of Person :-";
				cin>>no;

				if(i==1)
				{
					append(fname,lname,no);
					Display();
				}				
				else if(i==2)
				{
					insert(fname,lname,no);
					Display();
				}
				else
				{
					cout<<"\nEnter proper choice...!!!\n";
				}				
				break;
			case 2:
				Display();
				break;
			case 3:

				cout<<"\tEnter the FIRST NAME of Person that you want to EDIT :-";
				cin>>key;
				Edit(key);		
				Display();						
				break;
			case 4:		
				cout<<"\t1.Delete a link from FIRST\n\t2.Delete a link from LAST\n\tChoice :- ";
				cin>>i;		
				if(i==1)
				{
					Deletef();
					Display();
				}				
				else if(i==2)
				{
					Deletel();
					Display();
				}
				else
				{
					cout<<"\nEnter proper choice...!!!\n";
				}				
				break;

		}
	} while (choice!=100);
	return 0;
}
You never exit the switch instruction. You need to encase your switch case inside brackets {}. Only break instruction must be left out.

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
case 1:		
			      {cout<<"\t1.Append a link\n\t2.Attach at First\n\tChoice :- ";
				cin>>i;		
				cout<<"\tEnter the FIRST NAME of Person :-";
				cin>>fname;				
				cout<<"\tEnter the LAST NAME of Person :-";
				cin>>lname;
				cout<<"\tEnter the NEW PHONE NUMBER of Person :-";
				cin>>no;

				if(i==1)
				{
					append(fname,lname,no);
					Display();
				}				
				else if(i==2)
				{
					insert(fname,lname,no);
					Display();
				}
				else
				{
					cout<<"\nEnter proper choice...!!!\n";
				}	
                             }			
				break;
Last edited on
Topic archived. No new replies allowed.