Program is stopped

Hello everyone
My program stops working when working in 'Adding an item'. Program stops working. Would someone help me with this please ?

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//*********************************
// Libraries used for the program
//*********************************
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

//*********************************
// Linked list Structure for Movie
//*********************************
struct movie_node
{
string movie_name;
string cast_name[3];
string movie_genre;
int movie_rate;

movie_node* link;
};

//*****************************
// Main body of the program
//*****************************
int main ()
{
int choice, choice1;
string name, label, label1, cast_movies[3], choice2;

movie_node* nodeptr, *before_nodeptr;
movie_node* head;
head = new movie_node;

//*****************************************************
// Do-while loop to satisfy options from the Main Menu
//*****************************************************
do {

cout << " MAIN MENU :- "<< endl;
cout << endl;
cout << " 1) Removing an item. " << endl;
cout << " 2) Adding an item. " << endl;
cout << " 3) Displaying an item. " << endl;
cout << " 4) Searching an item. " << endl;
cout << " 5) Exit the program. " << endl;
cout << endl;

cout << " Enter your option out of these 5 options please : " << endl;
cin >> choice;

//***************************************************
// If condition to satisfy option number 1 from the Main Menu.
// Removing an item.
//***************************************************
if(choice == 1)
{

cout << " Enter the name of the movie/movies you want to remove ? " << endl;
cin >> name;

//***************************************************
// If condition to show if the list is empty or not.
//***************************************************
if (!head)
{
cout << " Empty list ! " << endl;
return -1;
}

//**************************************************************************
// If condition to check if the first node is the one to be deleted or not.
//**************************************************************************
if (head->movie_name == name)
{
nodeptr = head;
head = head->link;
delete nodeptr;
}

else
{
//*************************************************
// nodeptr is initialized to the head of the list.
//*************************************************
nodeptr = head;

while (nodeptr != NULL && nodeptr->movie_name != name)

before_nodeptr = nodeptr;
nodeptr = nodeptr->link;
}

if (nodeptr)
{

before_nodeptr-> link = nodeptr-> link;
delete nodeptr;

cout << " .............End............. " << endl;
}
}

//***************************************************
// Else if condition to satisfy option number 2 from the Main Menu.
// Adding an item.
//***************************************************
else if (choice == 2)
{
//***************************************
// Entering the name of the movie.
//***************************************
head = new movie_node;
cout << " Please enter the name of the movie you want to add or press '-5' to EXIT the program " << endl;
cin >> label;

if(label == "-5")
break;

//***************************************
// Entering the genre of the movie.
//***************************************
cout << " Enter your genre choice of the following genres:" << endl;
cout << " 1. ACTION " << endl;
cout << " 2. COMEDY " << endl;
cout << " 3. DRAMA " << endl;
cout << " 4. HORROR " << endl;
cout << " 5. MARTIAL ARTS " << endl;
cout << " 6. ROMANCE " << endl;
cout << " 7. SCIENCE FICTION " << endl;
cout << " 8. SUPER NATURAL " << endl;

cin >> choice2;
cout << endl;
cout << endl;

//******************************************
// Entering the actor names of the movie.
//****************************************
cout << " Enter 3 names of actors from this movie " << endl;
for (int a = 0; a < 3; a++)
{
cin >> cast_movies[a];
cout << endl;
cout << endl;
}

//***************************************
// Entering the movie rating.
//***************************************
cout << " Enter the movie rating:- 1, 2, 3, 4 or 5 stars " << endl;
cin >> choice1;
cout << endl;
cout << endl;

if (choice1 < 1 || choice1 >5)
{
cout << " Please re-enter 1, 2, 3, 4, or 5:" << endl;
cin >> choice1;
cout << endl;
}

if (head == NULL)
{

for (int a = 0; a < 3; a++)
head->cast_name[a] = cast_movies[a];

head-> movie_genre = choice2;
head-> movie_name = label;
head-> movie_rate = choice1;
}

else
{
movie_node* node = head;
while (node->link != NULL)

{
node = node-> link;
}
node = new movie_node;

for ( int a = 0; a < 3; a++)
node->cast_name[a] = cast_movies[a];

node->movie_genre = choice2;
node->movie_name = label;
node->movie_rate = choice1;
}
}

//***************************************************
// Else if condition to satisfy option number 3 from the Main Menu.
// Displaying an item.
//***************************************************
else if (choice == 3)
{
movie_node *current;
if (!head)
{
cout << " Empty list ! " << endl;
return-1;
}

else
{
cout << " The list contains -: " << endl;
while (current !=NULL)

{
cout << current->movie_name << endl;

for ( int a = 0; a < 3; a++)
cout << current-> cast_name[a] << endl;
cout << current-> movie_genre << endl;
cout << current-> movie_rate << endl;
cout << " *************** " << endl;

current = current->link ;
}
}

}

//***************************************************
// Else if condition to satisfy option number 4 from the Main Menu.
// Searching for an item.
//***************************************************
else if (choice == 4)
{
cout << " Enter the movie name you want to search for. " << endl;
cin >> label1;

movie_node* search = head;

//***************************************************
// If condition to check for an empty linked list.
//***************************************************
if (search == NULL)
{
return-1;
}

//***************************************************
// Moving to the next node for searching the movie.
//***************************************************
else
{
while (search->movie_name != label1 && search->link !=NULL)
{
search = search->link;
}

//***************************************************
// If condition to display the movie searched for.
//***************************************************
if (search->movie_name == label1)
{
cout << search->movie_genre << endl;
cout<< search->movie_name << endl;
cout<< search->movie_rate << endl;

for (int a = 0; a < 3; a++)
cout << search->cast_name[a] << endl;
}

else
{
return-1;
}

}

}

//***************************************************
// Else if condition to satisfy option number 5 from the Main Menu.
// Quitting the program.
//***************************************************
else if (choice == 5)
{
break;
}

//*********************************************************************************************
// Else condition to satisfy an option out from the options given from 1 to 5 in the Main Menu.
// User re-enters a choice between 1 to 5 from the Main Menu.
//*********************************************************************************************
else (choice <1 || choice >5);
cout << " Your choice made is incorrect. Please choose again an option. " << endl;
}

while ( choice >=1 && choice <=5);

//******************************************
// Pausing the system.
//******************************************
system ("pause");

return 0;
}// End of the main program. 

1
2
3
4
5
6
7
8
9
10
   if (head == NULL)
    {

        for (int a = 0; a < 3; a++)
            head->cast_name[a] = cast_movies[a];

        head->movie_genre = choice2;
        head->movie_name = label;
        head->movie_rate = choice1;
    }


If head is pointing to nothing, dereference it. Does that seem right to you?
I still do not get what is the case, here. I think it is something related to pointers. Could someone help me fix this ?
I still do not get what is the case, here.

When head is null you use it as if it is not. This results in undefined behavior. Don't do that.

You have a related problem in the else clause following where you also dereference a null pointer.
What cire is getting at is that dereferencing an empty pointer leads to undefined behaviour. In your case, the pointer that you're dereferencing (you're attempting to read the value that it points to) is NULL (the code that he quoted).

What you need to do is either assign that pointer to point to something or change
if (head == NULL)
to
if (head != NULL)

You also have similar problems elsewhere.
Topic archived. No new replies allowed.