Advertisement
COSCI539

CS136 Lab1 [7]

Feb 27th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.06 KB | None | 0 0
  1. //Lab 1 Wilson, Drayden T Th
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. const int INV_ARR_MAX = 15;
  11.  
  12. struct productInfo
  13. {
  14.     string ID, name;
  15.     int quantity;
  16.     float price;
  17. };
  18.  
  19. typedef productInfo product;
  20.  
  21. int FillInventory(product *list[]);
  22. void DuplicateArray(product *list1[], product *list2[], int listSize);
  23. void PrintList(product *list[], int listSize);
  24. int CompareItems(const void *item1, const void *item2);
  25. void TestSearchItem(product *list[], string testItem, string searchItem, int index);
  26. void SearchList(product *list[], int listSize, string searchItem);
  27. void PrintListInfo(product *list[], int listSize);
  28. void DeallocateMemory(product *list[], int listSize);
  29. void RuntimeError(const string errorMessage);
  30.  
  31. int main()
  32. {
  33.     product *pInventoryUnsorted[INV_ARR_MAX] = { nullptr },
  34.         *pInventorySorted[INV_ARR_MAX] = { nullptr };
  35.     int productCount, userMenuSelection = 0;
  36.     string searchItem;
  37.  
  38.     productCount = FillInventory(pInventoryUnsorted);
  39.  
  40.     cout << pInventoryUnsorted[0]->ID << ' ' << pInventoryUnsorted[0]->name << ' '
  41.         << pInventoryUnsorted[0]->quantity << ' ' << pInventoryUnsorted[0]->price << endl;
  42.     //////////////////// why does this work in FillInventory() but not here in main?
  43.  
  44.     DuplicateArray(pInventoryUnsorted, pInventorySorted, productCount);
  45.  
  46.     cout << "[1] Display the inventory unsorted.\n"
  47.         << "[2] Display the inventory in ascending order by a field.\n"
  48.         << "[3] Search for an item by ID or name.\n"
  49.         << "[4] Display the unique item count, total worth,"
  50.         << "and total item count of the inventory.\n"
  51.         << "[5] Exit.\n";
  52.  
  53.     do
  54.     {
  55.         if (userMenuSelection)
  56.         {
  57.             cout << "\nSelect another option.: ";
  58.         }
  59.         else
  60.         {
  61.             cout << "\nSelect an option by entering its corresponding number: ";
  62.         }
  63.  
  64.         cin >> userMenuSelection;
  65.  
  66.         switch (userMenuSelection)
  67.         {
  68.         case 1:
  69.             PrintList(pInventoryUnsorted, productCount);
  70.             break;
  71.         case 2:
  72.             qsort(pInventorySorted, productCount, sizeof(product*), CompareItems);
  73.             PrintList(pInventorySorted, productCount);
  74.             break;
  75.         case 3:
  76.             cout << "Input an ID or name to search by: ";
  77.             cin >> searchItem; //assuming input is all numbers or all letters
  78.  
  79.             transform(searchItem.begin(), searchItem.end(), searchItem.begin(), ::toupper);
  80.  
  81.             SearchList(pInventoryUnsorted, productCount, searchItem);
  82.             break;
  83.         case 4:
  84.             PrintListInfo(pInventoryUnsorted, productCount);
  85.             break;
  86.         case 5:
  87.             cout << "Exiting program.\n";
  88.             break;
  89.         default:
  90.             cout << "Invalid menu option selected.\n";
  91.  
  92.             userMenuSelection = 1; //avoids repeating initial instructions
  93.             break;
  94.         }
  95.     } while (userMenuSelection != 5);
  96.  
  97.     DeallocateMemory(pInventoryUnsorted, productCount);
  98.  
  99.     system("pause");
  100.     return 0;
  101. }
  102.  
  103. //Populates an array of pointers to structs with data from file
  104. //pre: list and inFile exist
  105. //post: inFile is closed, list contains items from inFile, number of items is returned
  106. int FillInventory(product *list[])
  107. {
  108.     ifstream inFile;
  109.     product temp;
  110.     int i; //counter
  111.  
  112.     inFile.open("input.txt");
  113.  
  114.     if (!inFile)
  115.     {
  116.         RuntimeError("Failed to open input file.");
  117.     }
  118.     else
  119.     {
  120.         cout << "Input file read successfully.";
  121.     }
  122.  
  123.  
  124.     for (i = 0; inFile >> temp.ID && i < INV_ARR_MAX; ++i)
  125.     {
  126.         inFile >> temp.name >> temp.quantity >> temp.price;
  127.  
  128.         transform((temp.name).begin(), (temp.name).end(), (temp.name).begin(), ::toupper);
  129.  
  130.         if (temp.quantity < 0 || temp.price < 0.01)
  131.         {
  132.             printf("Invalid entry detected. Discarding \"%s\" entry.\n", temp.name);
  133.         }
  134.         else
  135.         {
  136.             try
  137.             {
  138.                 list[i] = (product *)malloc(sizeof(product));
  139.             }
  140.             catch (bad_alloc exception)
  141.             {
  142.                 RuntimeError("Insufficient memory available.");
  143.             }
  144.  
  145.             list[i] = &temp;
  146.  
  147.             cout << list[i]->ID << ' ' << list[i]->name << ' ' << list[i]->quantity << ' ' << list[i]->price << endl;//////////////
  148.         }
  149.     }
  150.  
  151.     inFile.close();
  152.  
  153.     if (i == 0)
  154.     {
  155.         RuntimeError("Input file is empty.");
  156.     }
  157.     else if (!inFile.eof())
  158.     {
  159.         RuntimeError("Excess entries detected.");
  160.     }
  161.  
  162.     return i;
  163. }
  164.  
  165. //Assigns one array of pointers to structs to another
  166. //pre: list1 is populated and listSize is defined
  167. //post: list2 is identical to list1
  168. void DuplicateArray(product *list1[], product *list2[], int listSize)
  169. {
  170.     for (int i = 0; i < listSize; ++i)
  171.     {
  172.         list2[i] = list1[i];
  173.     }
  174. }
  175.  
  176. //Prints all members held within an array
  177. //pre: list is populated and listSize is defined
  178. //post: all members are printed
  179. void PrintList(product *list[], int listSize)
  180. {
  181.     for (int i = 0; i < listSize; ++i)
  182.     {
  183.         cout << list[i]->ID << ' '
  184.             << list[i]->name << ' '
  185.             << list[i]->quantity << ' '
  186.             << list[i]->price << endl;
  187.     }
  188. }
  189.  
  190. //Compares two members from different structs from an array of pointers to structs
  191. //pre: the source array and all structs within are defined
  192. //post: the difference between the two struct members is returned
  193. int CompareItems(const void *item1, const void *item2)
  194. {
  195.     product *pItem1 = *(product**)item1;
  196.     product *pItem2 = *(product**)item2;
  197.     int sortChoice;
  198.  
  199.     cout << "Select an option to sort the list by "    //sortChoice prompt and input must be here
  200.         << "[1]ID, [2]name, [3]quantity, [4]price: ";  //because of nature of qsort
  201.  
  202.     do
  203.     {
  204.         cin >> sortChoice;
  205.  
  206.         switch (sortChoice)
  207.         {
  208.         case 1:
  209.             return pItem1->ID.compare(pItem2->ID);
  210.         case 2:
  211.             return pItem1->name.compare(pItem2->name);
  212.         case 3:
  213.             return pItem1->quantity - pItem2->quantity;
  214.         case 4:
  215.             return pItem1->price - pItem2->price;
  216.         default:
  217.             cout << "Invalid option selected. Try Again: ";
  218.             break;
  219.         }
  220.     } while (sortChoice < 1 || sortChoice > 4);
  221. }
  222.  
  223. //Tests if a given item is equivalent to a given search term
  224. //pre: all variables are defined
  225. //post: item info is printed if found, fail message printed otherwise
  226. void TestSearchItem(product *list[], string testItem, string searchItem, int index)
  227. {
  228.     if (testItem == searchItem)
  229.     {
  230.         cout << list[index]->ID << ' '
  231.             << list[index]->name << ' '
  232.             << list[index]->quantity << ' '
  233.             << list[index]->price << endl;
  234.     }
  235.     else
  236.     {
  237.         cout << "Item not found.\n";
  238.     }
  239. }
  240.  
  241. //Seaches a list for a given item by ID or name then prints that item's info is found
  242. //pre: list is populated, listSize is defined and searchItem is defined
  243. //post: the sought after item's info is printed if found
  244. void SearchList(product *list[], int listSize, string searchItem)
  245. {
  246.     for (int i = 0; i < listSize; ++i)
  247.     {
  248.         if (isdigit(searchItem[0])) //search item is an ID
  249.         {
  250.             TestSearchItem(list, list[i]->ID, searchItem, i);
  251.         }
  252.         else //search item is a name
  253.         {
  254.             TestSearchItem(list, list[i]->name, searchItem, i);
  255.         }
  256.  
  257.     }
  258. }
  259.  
  260. //Calculates and prints the number of unique elements, total al value and total size of an inventory
  261. //pre: list is populated and listSize is defined
  262. //post: info has been printed
  263. void PrintListInfo(product *list[], int listSize)
  264. {
  265.     static float inventoryValue = 0;
  266.     static int inventorySize = 0;
  267.     bool infoCalculated = 0;
  268.  
  269.     for (int i = 0; i < listSize && !infoCalculated; ++i)
  270.     {
  271.         inventoryValue += list[i]->price;
  272.         inventorySize += list[i]->quantity;
  273.     }
  274.  
  275.     infoCalculated = 1;
  276.  
  277.     cout << "Unique Items: " << listSize
  278.         << "\nInventory Value: " << inventoryValue
  279.         << "\nInventory Size: " << inventorySize << endl;
  280. }
  281.  
  282. //Frees memory of an array's elements
  283. //pre: list is populated and listSize is defined
  284. //post: list's elements no longer have memory allocated to them
  285. void DeallocateMemory(product *list[], int listSize)
  286. {
  287.     for (int i = 0; i < listSize; ++i)
  288.     {
  289.         free((void *)list[i]);
  290.     }
  291. }
  292.  
  293. //Outputs a unique error message and ends the program
  294. //pre: N/A
  295. //post: error message has been output and program has ended
  296. void RuntimeError(const string errorMessage)
  297. {
  298.     cout << "Error: " << errorMessage << " Exiting program.\n\n";
  299.     system("pause");
  300.     exit(1);
  301. }
  302.  
  303. //issue with product count after fill
  304. //switch to bubble sort
  305. //program crashes when trying to exit from menu
  306. //store price as float?
  307.  
  308. /*
  309. 997196478 StroLLer 25 134.78
  310. 168484964 umBRElLA 39 14.74
  311. 168746844 Coat 17 36.54
  312. 498749896 baSkEt 29 7.23
  313. 836749194 tOwEl 52 5.89
  314. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement