menu driven program

I'm trying to create a menu driven program using arrow operators and linked lists to track/update a list of movies. I think I already solved most of the code but I am having trouble updating and removing the node attributes (updating and removing from the movie list). I see am having many errors within the ' void update_movie(movie* head, int size) ' and the ' void remove_movie(movie* head, int* size) ' functions (line 109 to line 155) and I still don't know what I am doing incorrectly. Any help is greatly appreciated, thank you.

my 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
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
#include <iostream>
#include <iomanip>
#include <cstdlib> 
using namespace std;

struct movie
{
	string title;
	int viewed_year;
	int rating;
	movie* next;
};

void update_movie(movie*, int);
void remove_movie(movie*, int*);
void list_movie(const movie*);
void arrange_by_title(movie*);
void arrange_by_rating(movie* );
void arrange_by_viewed(movie* );
void print_menu(); // prints main menu

// head pointer is "firstmovie"

int main()
{
	int size = 0;
	char choice;
	movie* firstmovie = 0; 

	string title1, title2, title3; // 3 Strings to store names of the movies
	int viewed_year1, viewed_year2, viewed_year3; // 3 ints's to store viewed year of the movies
	int rating1, rating2, rating3; // 3 ints's to store rating of the movies

	// 3 movie objects to store the movies
	movie a = { title1, viewed_year1, rating1 };
	movie b = { title2, viewed_year2, rating2 };
	movie c = { title3, viewed_year3, rating3 };

	// add the three nodes into the linked list

	a.next = firstmovie;
	firstmovie = &a;
	size++;
	b.next = firstmovie;
	firstmovie = &b;
	size++;
	c.next = firstmovie;
	firstmovie = &c;
	size++;

	print_menu();
	cin >> choice; 

	while (choice != 'Q' && choice != 'q') // && means 'and' operator
	{
		switch (choice) {
		case 'U':
		case 'u':
			update_movie(firstmovie, size);
			break;

		case 'E':
		case 'e':
			remove_movie(firstmovie, &size);
			break;

		case 'L':
		case 'l':
			list_movie(firstmovie);
			break;

		case 'T':
		case 't':
			arrange_by_title(firstmovie);
			break;

		case 'V':
		case 'v':
			arrange_by_viewed(firstmovie);
			break;

		case 'R':
		case 'r':
			arrange_by_rating(firstmovie);
			break;

		default:
			cout << "**Invalid choice, please enter again**" << endl;
			break;
		}
		print_menu();
		cin >> choice;

	}
}
void print_menu()
{
	cout << "| Menu |\n" << endl;
	cout << "U Update a movie" << endl;
	cout << "E rEmove a movie" << endl;
	cout << "L List all movies" << endl;
	cout << "T arrange by Title" << endl;
	cout << "V arrange by year Viewed" << endl;
	cout << "R arrange by Rating" << endl;
	cout << "Q Quit" << endl;
	cout << "...your choice:";
}

void update_movie(movie* head, int size) // Help here please 
{
	cout << "****Update Movie Menu" << endl;
	
	movie* firstmovie = 0;
	movie*p; 
	for (p = head; p; p = p->next)
		if (p->title == "q") break;

	if (p)
	{
		if (head == p) head = p->next;
		cout << "Which movie to update [1-3]: ";
		cin >> p->title;
		cout << "Enter an updated name for " << p->title << " :";
		cin >> p->title;
		cout << "Enter the year you saw " << p->viewed_year << " [like 2016]:";
		cin >> buf; p->viewed_year = atoi(buf);
		cout << "Enter the rating for " << p << " [1, 2, 3, 4, 5]:";
		cin >> buf; p->rating = atoi(buf);
	}

	else
		cout << "**Invalid choice, please enter again**";

	cout << "\nUpdated movie list after updating" << endl;
	print_menu();
	
}

 void remove_movie(movie* head, int* size) // Help here also
{	
	cout << "****Remove Movie Menu" << endl;
	movie*p , *prev;
	for (p = head, prev = 0; p; prev = p, p = p->next)
		if (p->title == "q" && "Q" ) break;
		
	if (p)
	{
		if (prev) prev->next = p->next;
		else head = p->next;
	}

	else
		cout << "**Invalid choice, please enter again**";
}

 void list_movie(const movie* head)  // prints list of movies
{
	cout << "****List Movie Menu" << endl;
	cout << "#" << setw(15) << "Movie Title" << setw(8) << "Viewed" << setw(8) << "Rating" << endl;
	cout << "==========================================" << endl;

	int i = 1;
	for (const movie* p = head; p; p = p->next)
	{
		cout << i++;
		cout << setw(15) << p->title;
		cout << setw(6) << p->viewed_year;
		cout << setw(6) << p->rating << endl;
	}

}

 void arrange_by_title(movie* head) // prints movies arranged by title
{
	cout << "****Arrange Movie By Title Menu" << endl;
	for (movie* p = head; p; p = p->next)
		for (movie* q = p->next; q; q = q->next)
			if (q->title < p->title)
			{
				swap(*p, *q);
				swap(p->next, q->next);
			}
}

 void arrange_by_rating(movie* head) // prints movies arranged by rating
{
	cout << "****Arrange Movie By Rating Menu" << endl;
	for (movie* p = head; p; p = p->next)
		for (movie* q = p->next; q; q = q->next)
			if (q->rating < p->rating)
			{
				swap(*p, *q);
				swap(p->next, q->next);
			}
}

 void arrange_by_viewed(movie* head) // prints movies arranged by year viewed
{
	cout << "****Arrange Movie By Viewed Menu" << endl;
	for (movie* p = head; p; p = p->next)
		for (movie* q = p->next; q; q = q->next)
			if (q->viewed_year < p->viewed_year)
			{
				swap(*p, *q);
				swap(p->next, q->next);
			}
}
Last edited on
Hello tadinaaawr,

I believe you problem starts with lines 41 - 48.

Line 41 a.next = firstmovie;, but "firstmovie" was initialized to zero, should be "nullptr", and the actual address should be the address of "b".

line 42 That makes sense here. A lot of time this variable is called head, but "firstmovie" works.

line 44 is b.next = firstmovie;, but "firstmovie" has the address of "a" when it should be "c".

Line 47 Same concept as line 44.

line 48 sets "firstmovie to the address of "c". At this point "firstmovie" as the name implies should have the address of "a" not "c".

Now in the switch you are calling the functions with "firstmovie" which has the address of "c" and has no where to go or if is an update or delete function it could have the wrong movie to work with.

It looks to me that the linked list is where the problem is. I say that based on what I see in your code. It will be about an hour before I can load the progrm and test it.

Hope that helps,

Andy
Topic archived. No new replies allowed.