Advertisement
Checosnake

Quiz Add Function

Feb 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. DList::DList()
  2. {
  3.     item_list = NULL;
  4.     item_amounts = NULL;
  5.     num_items = 0;
  6. }
  7.  
  8. DList::DList(string item, int amount)
  9. {
  10.     //Check to see if Lists are empty
  11.     if (num_items < 0)
  12.     {
  13.         item_list = new item_list[num_items+1];
  14.         item_amounts = new item_amounts[num_items+1];
  15.         item_list[num_items] = item;
  16.         item_amounts[num_items] = amount;
  17.     }
  18.     //if list is not empty
  19.     else
  20.     {
  21.         //Create 2 temporary arrays to store previous values
  22.         string *tempa = new tempa[num_items+1];
  23.         int *tempb = new tempb[num_items+1];
  24.        
  25.         /*Cycle through existing arrays to copy values and leave
  26.         one empty cell at the end of each temp array*/
  27.         for (int i =0; i < num_items; i++)
  28.         {
  29.             tempa[i] = item_list[i];
  30.             tempb[i] = item_amounts[i];
  31.         }
  32.        
  33.         //Assign given values to last cell in temp arrays
  34.         tempa[num_items+1] = item;
  35.         tempb[num_items+1] = amount;
  36.        
  37.         //Deletes old arrays to create bigger ones
  38.         delete[] item_list;
  39.         delete[] item_amounts;
  40.        
  41.         //Create new 1-cell-larger arrays
  42.         item_list = new item_list[num_items+1];
  43.         item_amounts = new item_amounts[num_items+1];
  44.        
  45.         //Copy temp values to new larger arrays
  46.         for (int i =0; i < num_items+1; i++)
  47.         {
  48.             item_list[i] = tempa[i];
  49.             item_amounts[i] = tempb[i];
  50.         }
  51.        
  52.         //Deletes temporary arrays
  53.         delete[] tempa;
  54.         delete[] tempb;
  55.        
  56.         //Sets Number of items to +1
  57.         num_items++;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement