Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 1 Wilson, Drayden T Th
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <string>
- using namespace std;
- const int INV_ARR_MAX = 15;
- struct productInfo
- {
- string ID, name;
- int quantity;
- float price;
- };
- typedef productInfo product;
- int FillInventory(product *list[]);
- void DuplicateArray(product *list1[], product *list2[], int listSize);
- void PrintList(product *list[], int listSize);
- int CompareItems(const void *item1, const void *item2);
- void TestSearchItem(product *list[], string testItem, string searchItem, int index);
- void SearchList(product *list[], int listSize, string searchItem);
- void PrintListInfo(product *list[], int listSize);
- void DeallocateMemory(product *list[], int listSize);
- void RuntimeError(const string errorMessage);
- int main()
- {
- product *pInventoryUnsorted[INV_ARR_MAX] = { nullptr },
- *pInventorySorted[INV_ARR_MAX] = { nullptr };
- int productCount, userMenuSelection = 0;
- string searchItem;
- productCount = FillInventory(pInventoryUnsorted);
- cout << pInventoryUnsorted[0]->ID << ' ' << pInventoryUnsorted[0]->name << ' '
- << pInventoryUnsorted[0]->quantity << ' ' << pInventoryUnsorted[0]->price << endl;
- //////////////////// why does this work in FillInventory() but not here in main?
- DuplicateArray(pInventoryUnsorted, pInventorySorted, productCount);
- cout << "[1] Display the inventory unsorted.\n"
- << "[2] Display the inventory in ascending order by a field.\n"
- << "[3] Search for an item by ID or name.\n"
- << "[4] Display the unique item count, total worth,"
- << "and total item count of the inventory.\n"
- << "[5] Exit.\n";
- do
- {
- if (userMenuSelection)
- {
- cout << "\nSelect another option.: ";
- }
- else
- {
- cout << "\nSelect an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection;
- switch (userMenuSelection)
- {
- case 1:
- PrintList(pInventoryUnsorted, productCount);
- break;
- case 2:
- qsort(pInventorySorted, productCount, sizeof(product*), CompareItems);
- PrintList(pInventorySorted, productCount);
- break;
- case 3:
- cout << "Input an ID or name to search by: ";
- cin >> searchItem; //assuming input is all numbers or all letters
- transform(searchItem.begin(), searchItem.end(), searchItem.begin(), ::toupper);
- SearchList(pInventoryUnsorted, productCount, searchItem);
- break;
- case 4:
- PrintListInfo(pInventoryUnsorted, productCount);
- break;
- case 5:
- cout << "Exiting program.\n";
- break;
- default:
- cout << "Invalid menu option selected.\n";
- userMenuSelection = 1; //avoids repeating initial instructions
- break;
- }
- } while (userMenuSelection != 5);
- DeallocateMemory(pInventoryUnsorted, productCount);
- system("pause");
- return 0;
- }
- //Populates an array of pointers to structs with data from file
- //pre: list and inFile exist
- //post: inFile is closed, list contains items from inFile, number of items is returned
- int FillInventory(product *list[])
- {
- ifstream inFile;
- product temp;
- int i; //counter
- inFile.open("input.txt");
- if (!inFile)
- {
- RuntimeError("Failed to open input file.");
- }
- else
- {
- cout << "Input file read successfully.";
- }
- for (i = 0; inFile >> temp.ID && i < INV_ARR_MAX; ++i)
- {
- inFile >> temp.name >> temp.quantity >> temp.price;
- transform((temp.name).begin(), (temp.name).end(), (temp.name).begin(), ::toupper);
- if (temp.quantity < 0 || temp.price < 0.01)
- {
- printf("Invalid entry detected. Discarding \"%s\" entry.\n", temp.name);
- }
- else
- {
- try
- {
- list[i] = (product *)malloc(sizeof(product));
- }
- catch (bad_alloc exception)
- {
- RuntimeError("Insufficient memory available.");
- }
- list[i] = &temp;
- cout << list[i]->ID << ' ' << list[i]->name << ' ' << list[i]->quantity << ' ' << list[i]->price << endl;//////////////
- }
- }
- inFile.close();
- if (i == 0)
- {
- RuntimeError("Input file is empty.");
- }
- else if (!inFile.eof())
- {
- RuntimeError("Excess entries detected.");
- }
- return i;
- }
- //Assigns one array of pointers to structs to another
- //pre: list1 is populated and listSize is defined
- //post: list2 is identical to list1
- void DuplicateArray(product *list1[], product *list2[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- list2[i] = list1[i];
- }
- }
- //Prints all members held within an array
- //pre: list is populated and listSize is defined
- //post: all members are printed
- void PrintList(product *list[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- cout << list[i]->ID << ' '
- << list[i]->name << ' '
- << list[i]->quantity << ' '
- << list[i]->price << endl;
- }
- }
- //Compares two members from different structs from an array of pointers to structs
- //pre: the source array and all structs within are defined
- //post: the difference between the two struct members is returned
- int CompareItems(const void *item1, const void *item2)
- {
- product *pItem1 = *(product**)item1;
- product *pItem2 = *(product**)item2;
- int sortChoice;
- cout << "Select an option to sort the list by " //sortChoice prompt and input must be here
- << "[1]ID, [2]name, [3]quantity, [4]price: "; //because of nature of qsort
- do
- {
- cin >> sortChoice;
- switch (sortChoice)
- {
- case 1:
- return pItem1->ID.compare(pItem2->ID);
- case 2:
- return pItem1->name.compare(pItem2->name);
- case 3:
- return pItem1->quantity - pItem2->quantity;
- case 4:
- return pItem1->price - pItem2->price;
- default:
- cout << "Invalid option selected. Try Again: ";
- break;
- }
- } while (sortChoice < 1 || sortChoice > 4);
- }
- //Tests if a given item is equivalent to a given search term
- //pre: all variables are defined
- //post: item info is printed if found, fail message printed otherwise
- void TestSearchItem(product *list[], string testItem, string searchItem, int index)
- {
- if (testItem == searchItem)
- {
- cout << list[index]->ID << ' '
- << list[index]->name << ' '
- << list[index]->quantity << ' '
- << list[index]->price << endl;
- }
- else
- {
- cout << "Item not found.\n";
- }
- }
- //Seaches a list for a given item by ID or name then prints that item's info is found
- //pre: list is populated, listSize is defined and searchItem is defined
- //post: the sought after item's info is printed if found
- void SearchList(product *list[], int listSize, string searchItem)
- {
- for (int i = 0; i < listSize; ++i)
- {
- if (isdigit(searchItem[0])) //search item is an ID
- {
- TestSearchItem(list, list[i]->ID, searchItem, i);
- }
- else //search item is a name
- {
- TestSearchItem(list, list[i]->name, searchItem, i);
- }
- }
- }
- //Calculates and prints the number of unique elements, total al value and total size of an inventory
- //pre: list is populated and listSize is defined
- //post: info has been printed
- void PrintListInfo(product *list[], int listSize)
- {
- static float inventoryValue = 0;
- static int inventorySize = 0;
- bool infoCalculated = 0;
- for (int i = 0; i < listSize && !infoCalculated; ++i)
- {
- inventoryValue += list[i]->price;
- inventorySize += list[i]->quantity;
- }
- infoCalculated = 1;
- cout << "Unique Items: " << listSize
- << "\nInventory Value: " << inventoryValue
- << "\nInventory Size: " << inventorySize << endl;
- }
- //Frees memory of an array's elements
- //pre: list is populated and listSize is defined
- //post: list's elements no longer have memory allocated to them
- void DeallocateMemory(product *list[], int listSize)
- {
- for (int i = 0; i < listSize; ++i)
- {
- free((void *)list[i]);
- }
- }
- //Outputs a unique error message and ends the program
- //pre: N/A
- //post: error message has been output and program has ended
- void RuntimeError(const string errorMessage)
- {
- cout << "Error: " << errorMessage << " Exiting program.\n\n";
- system("pause");
- exit(1);
- }
- //issue with product count after fill
- //switch to bubble sort
- //program crashes when trying to exit from menu
- //store price as float?
- /*
- 997196478 StroLLer 25 134.78
- 168484964 umBRElLA 39 14.74
- 168746844 Coat 17 36.54
- 498749896 baSkEt 29 7.23
- 836749194 tOwEl 52 5.89
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement