Advertisement
cperryoh

Untitled

Feb 28th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. // inventorySystem.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. //function that takes in an id and a price and checks if they are valid
  8. bool checkIfItemIsvalid(int id, float price);
  9. void invalidInput(std::string invalidItem);
  10. void printItems(int ids[], float prices[], int itemCount);
  11. void printIdAndPrice(int id, int price);
  12. int main()
  13. {
  14.     const int ITEM_COUNT = 5;
  15.     int IDs[ITEM_COUNT];
  16.     float prices[ITEM_COUNT];
  17.  
  18.     //separator varible
  19.     char separator = ',';
  20.  
  21.     //instructions for id and price entry
  22.     cout << "Please enter the 5 inventory item id numbers and prices in the following format\n[id],[price]\n*ENTER*\n[id],[price]\nEx:\n123,4.56\n789,1.11\n...\n";
  23.     //id and price entry
  24.     for (int i = 0; i < ITEM_COUNT; i++) {
  25.         int id;
  26.         float price;
  27.         cin >> id >> separator >> price;
  28.         while (!checkIfItemIsvalid(id,price)) {
  29.             cin >> id >> separator >> price;
  30.         }
  31.         IDs[i] = id;
  32.         prices[i] = price;
  33.     }
  34.     printItems(IDs, prices, ITEM_COUNT);
  35. }
  36. void printItems(int ids[], float prices[],int itemCount) {
  37.     for (int i = 0; i < itemCount; i++) {
  38.         cout << (i + 1) << ") id: " << ids[i] << " price: " << prices[i]<<"\n";
  39.     }
  40. }
  41. void printIdAndPrice(int id, int price) {
  42.     cout << "1) id: " << id << " price: " << price << endl;
  43. }
  44. void invalidInput(std::string invalidItem) {
  45.     cout << "Error: The "<< invalidItem <<" that you have entered is invalid, please try again.\n";
  46.    
  47. }
  48. bool checkIfItemIsvalid(int id, float price) {
  49.     /*
  50.     * id: id to check if it is valid
  51.     * price: price to check if it is valid
  52.     */
  53.  
  54.     //check id
  55.     if (id <= 0) {
  56.         invalidInput("id");
  57.         return false;
  58.     }
  59.  
  60.     //check price
  61.     if (price <= 0) {
  62.         invalidInput("price");
  63.         return false;
  64.     }
  65.     return true;
  66. }
  67. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  68. // Debug program: F5 or Debug > Start Debugging menu
  69.  
  70. // Tips for Getting Started:
  71. //   1. Use the Solution Explorer window to add/manage files
  72. //   2. Use the Team Explorer window to connect to source control
  73. //   3. Use the Output window to see build output and other messages
  74. //   4. Use the Error List window to view errors
  75. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  76. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement