468,167 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,167 developers. It's quick & easy.

Rental Movie Program

2 2Bits
Hi, I am a student at a university in the Philippines and I wonder if you guys could help me with a problem that I cannot solve. I'm kind of weak in the algorithm. And I really want to improve it. I hope you guys can help me. This is the problem

Create a MovieList Class with the following components:
Movie Code
Movie Title
Movie Genre
Year Released

The program requires the following menu and processes:
There will be 5 Movie Information on the Linked List.
This will be done in the constructor.
Insert a New Movie (insert operation)
Rent a Movie (delete operation)
Return a Movie (append operation)
Show Movie Details (traversal operation with match Movie Code)
Print Movie List (traversal operation)
Quit the Program (destructor)

I hope anyone can help me
Feb 6 '21 #1
4 1819
dev7060
413 Expert 256MB
The given context states the blueprint clearly. What have you done so far? Where are you stuck at?
Feb 6 '21 #2
ryan198913
2 2Bits
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //implementation
  8.  
  9. class FloatList
  10.     {
  11.        private:
  12.            struct ListNode
  13.            {
  14.                string name;
  15.                string genre;
  16.                int code;
  17.                int year;
  18.                struct ListNode *next;
  19.            };
  20.            ListNode *head;
  21.        public:
  22.            FloatList();
  23.            ~FloatList();
  24.            void appendNode(string name,
  25.                string genre,
  26.                int code,
  27.                int year);
  28.            void insertNode(string name,
  29.                string genre,
  30.                int code,
  31.                int year);
  32.            void deleteNode(string name,
  33.                string genre,
  34.                int code,
  35.                int year);
  36.            void displayList();
  37.     };
  38.  
  39. // FUNCTIONS
  40.  
  41. FloatList::FloatList()
  42. {
  43.     head = NULL;
  44. }
  45.  
  46.  
  47. FloatList::~FloatList()
  48. {
  49.     ListNode *nodePtr, *nextNode;
  50.  
  51.     nodePtr = head;
  52.     while (nodePtr != NULL)
  53.     {
  54.         nextNode = nodePtr->next;
  55.         delete nodePtr;
  56.         nodePtr = nextNode;
  57.     }
  58.     cout << "Linked List has been destroyed!" << endl;
  59. }
  60.  
  61. void FloatList::appendNode(string Name, string Genre, int Code, int Year)
  62. {
  63.     ListNode *newNode, *nodePtr;
  64.     // Allocate a new node & store num
  65.     newNode = new ListNode;
  66.     newNode->name = Name;
  67.     newNode->next = NULL;
  68.     newNode->genre = Genre;
  69.     newNode->next = NULL;
  70.     newNode->code = Code;
  71.     newNode->next = NULL;
  72.     newNode->year = Year;
  73.     newNode->next = NULL;
  74.  
  75.     // If there are no nodes in the list
  76.     // make newNode the first node
  77.     if (!head)
  78.         head = newNode;
  79.     else    // Otherwise, insert newNode at end
  80.     {
  81.         // Initialize nodePtr to head of list
  82.         nodePtr = head;
  83.         // Find the last node in the list
  84.         while (nodePtr->next)
  85.             nodePtr = nodePtr->next;
  86.         // Insert newNode as the last node
  87.         nodePtr->next = newNode;
  88.     }
  89.     cout << endl << "Input has been successfully Appended!" << endl;
  90. }
  91.  
  92.  
  93. void FloatList::insertNode(string Name, string Genre, int Code, int Year)
  94. {
  95.     ListNode *newNode, *nodePtr, *previousNode;
  96.  
  97.     // Allocate a new node & store Num
  98.     newNode = new ListNode;
  99.     newNode->name = Name;
  100.     newNode->genre = Genre;
  101.     newNode->code = Code;
  102.     newNode->year = Year;
  103.  
  104.     // If there are no nodes in the list
  105.     // make newNode the first node
  106.     if (!head)
  107.     {
  108.         head = newNode;
  109.         newNode->next = NULL;
  110.     }
  111.     else    // Otherwise, insert newNode.
  112.     {
  113.         // Initialize nodePtr to head of list
  114.         nodePtr = head;
  115.         previousNode = NULL;
  116.  
  117.         // Skip all nodes whose value member is less
  118.         // than num.
  119.         while (nodePtr != NULL && nodePtr->name < Name)
  120.         {
  121.             previousNode = nodePtr;
  122.             nodePtr = nodePtr->next;
  123.         }
  124.         // If the new mode is to be the 1st in the list,
  125.             // insert it before all other nodes.
  126.         if (previousNode == NULL)
  127.         {
  128.             head = newNode;
  129.             newNode->next = nodePtr;
  130.         }
  131.         else
  132.         {
  133.             previousNode->next = newNode;
  134.             newNode->next = nodePtr;
  135.         }
  136.     }
  137.     cout << endl << "Input has been successfully Inserted!" << endl;
  138. }
  139.  
  140.  
  141. void FloatList::displayList()
  142. {
  143.     ListNode *nodePtr;
  144.     if (head == NULL)
  145.         cout << "The list is empty!" << endl;
  146.     else
  147.     {
  148.         cout << "The nodes in the List are... " << endl;
  149.         nodePtr = head;
  150.         while (nodePtr)
  151.         {
  152.             cout << nodePtr->name << endl;
  153.             cout << nodePtr->genre << endl;
  154.             cout << nodePtr->code << endl;
  155.             cout << nodePtr->year << endl;
  156.             nodePtr = nodePtr->next;
  157.             cout<< endl;
  158.         }
  159.     }
  160. }
  161.  
  162. int menu();
  163. int main()
  164. {
  165.     FloatList list;
  166.  
  167.     string name;
  168.     string genre;
  169.     int code;
  170.     int year;
  171.  
  172.     int ch;
  173.  
  174.     do
  175.     {
  176.         ch = menu();
  177.         //system("clear");
  178.         if (ch == 1)
  179.         {
  180.             cout << " << Appending a Node >> " << endl << endl;
  181.             cout << "Enter Movie Name: " << endl;
  182.             getline(cin, name);        
  183.             cout << "Enter Movie Name: " << endl;
  184.                 getline(cin, genre);
  185.             cout << "Enter Movie Name: " << endl;
  186.             cin >> code;
  187.             cout << "Enter Movie Name: " << endl;
  188.             cin >> year;
  189.             list.insertNode(name, genre, code, year);
  190.         }
  191.         else if (ch == 2)
  192.         {
  193.             cout << " << Type Movie Information >> " << endl << endl;
  194.             cout << "Enter Movie Name, Genre, Code, Year" << endl;
  195.             cin >> name >> genre >> code >> year;
  196.             list.insertNode(name, genre, code, year);
  197.         }
  198.         else if (ch == 3)
  199.         {
  200.             cout << " << Traversing the List >> " << endl << endl;
  201.             list.displayList();
  202.       system("pause");
  203.         }
  204.         else if (ch == 4)
  205.         {
  206.             cout << "Thank you for using the program!" << endl; 
  207.             list.~FloatList();
  208.             exit(0);
  209.         }
  210.         else
  211.         {
  212.             cout << "Invalid Input!" << endl << endl;
  213.             break;
  214.         }
  215.         cout << endl;
  216.     } while (ch >= 1 && ch <= 3);
  217. }
  218.  
  219. //Main
  220.  
  221. int menu()
  222. {
  223.     int ch;
  224.  
  225.     system("cls");
  226.  
  227.     cout << " << Linked List Demonstration >> " << endl << endl;
  228.     cout << "[1] Append a Node" << endl;
  229.     cout << "[2] Inserting a Node" << endl;
  230.     cout << "[3] Deleting a Node" << endl;
  231.     cout << "[4] Traversing the List" << endl;
  232.     cout << "[5] Quit the Program" << endl << endl;
  233.     cout << "Enter Choice: ";
  234.     cin >> ch;
  235.     cout<< endl;
  236.     return(ch);
  237. }
Whenever I tried to get title and genre with SPACE it automatically says that the "Input is a invalid" and "The list has been destroyed".
Feb 7 '21 #3
dev7060
413 Expert 256MB
Whenever I tried to get title and genre with SPACE it automatically says that the "Input is a invalid" and "The list has been destroyed".
Does the title or genre have spaces in them?
Assuming the choice is 2,
Expand|Select|Wrap|Line Numbers
  1. cin >> name >> genre >> code >> year;
cin skips the leading white spaces -> starts reading when it finds the first non-whitespace character -> skips the leading white spaces ...

Example input: Avengers: Age of Ultron Action/Sci-fi 1001 2015
name = Avengers:
genre = Age
code = of
year = Ultron
Use if(!cin) to check the "didn't work" state.
Feb 7 '21 #4
Banfa
9,033 Expert Mod 8TB
I assume you get different responses for options 1 and 2; you have coded them very differently and I image option 1 works and option 2 doesn't. However they do the same thing, more or less, including option 1 calling insertNode when I suspect it should be calling appendNode.
Feb 9 '21 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.