Write a menu-driven C++ program to track your favorite movies in C++ string usin
ID: 3597760 • Letter: W
Question
Write a menu-driven C++ program to track your favorite movies in C++ string using Linked Lists.
Sample Output:
Requirements:
-Use “switch” statement to process the user’s input to go to the sub-menu.
- Allow for movie titles up to 30 characters in length.
- Do case-independent sorting, but do not convert the case of movie titles when they are entered using cin.
- You decide upon your rating system -- can be any data type.
- Use a linked list to store the movie information, initially empty, but with three nodes added before the main loop starts.
- The output table should have nicely-spaced column headings with sequence numbers.
- Use sequence numbers for the update and remove options.
- If an out-of-range sequence number is entered for either the remove or update options, either ignore it or output a warning -- your choice.
- Output blank lines to separate blocks of text as modeled in the sample output above.
Limitations:
We cannot add nodes, because nodes have to have been already declared in order to be in the linked list.
So do this -- start with 3 or more nodes, initialized to generic movie titles (like movie1). Put them all in the list before starting the menu loop. Once a node is removed, it's gone!
Keeping Track Of The Number Of Nodes:
Did you notice the "Which movie to update [1-3]:" prompt? It's not as easy as it looks! What if a node has been removed? Then it's "[1-2]". You'll need a way to keep track of the number of nodes in your list, and a way to put that number in the update and remove prompts. It's not too hard if you think about it and plan it out.
Do not save the sequence number as an attribute of the movie objects!
---------------------------------------------------------------------
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 with the ' void update_movie(movie* head, int size) ' and the ' void remove_movie(movie* head, int* size) ' functions and I still don't know what I am doing incorrectly. Any help is greatly appreciated, thank you.
Here is my code so far:
---------------------------------------------------------------------
#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; // an empty linked list
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; // user inputs any Char
while (choice != 'Q' && choice != 'q')
{
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 | " << 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*p;
for (p = head; p; p = p->next)
if (p->title == "q" || "Q") break;
if (p)
{
char buf[100];
cout << "Which movie to update [1-3]: ";
cin >> buf; p->title = atoi(buf);
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->title << " [1, 2, 3, 4, 5]:";
cin >> buf; p->rating = atoi(buf);
}
else
cout << "**Invalid choice, please enter again**";
cout << " Updated movie list after updating" << endl;
print_menu();
}
void remove_movie(movie* head, int* size) // Help here please :(
{
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;
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);
}
}
Explanation / Answer
#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; // an empty linked list
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; // user inputs any Char
while (choice != 'Q' && choice != 'q')
{
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 | " << 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*p;
for (p = head; p; p = p->next)
if (p->title == "q" || "Q") break;
if (p)
{
char buf[100];
cout << "Which movie to update [1-3]: ";
cin >> buf; p->title = atoi(buf);
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->title << " [1, 2, 3, 4, 5]:";
cin >> buf; p->rating = atoi(buf);
}
else
cout << "**Invalid choice, please enter again**";
cout << " Updated movie list after updating" << endl;
print_menu();
}
void remove_movie(movie* head, int* size) // Help here please :(
{
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;
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.