Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // inventorySystem.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- using namespace std;
- //function that takes in an id and a price and checks if they are valid
- bool checkIfItemIsvalid(int id, float price);
- void invalidInput(std::string invalidItem);
- void printItems(int ids[], float prices[], int itemCount);
- void printIdAndPrice(int id, int price);
- int main()
- {
- const int ITEM_COUNT = 5;
- int IDs[ITEM_COUNT];
- float prices[ITEM_COUNT];
- //separator varible
- char separator = ',';
- //instructions for id and price entry
- 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";
- //id and price entry
- for (int i = 0; i < ITEM_COUNT; i++) {
- int id;
- float price;
- cin >> id >> separator >> price;
- while (!checkIfItemIsvalid(id,price)) {
- cin >> id >> separator >> price;
- }
- IDs[i] = id;
- prices[i] = price;
- }
- printItems(IDs, prices, ITEM_COUNT);
- }
- void printItems(int ids[], float prices[],int itemCount) {
- for (int i = 0; i < itemCount; i++) {
- cout << (i + 1) << ") id: " << ids[i] << " price: " << prices[i]<<"\n";
- }
- }
- void printIdAndPrice(int id, int price) {
- cout << "1) id: " << id << " price: " << price << endl;
- }
- void invalidInput(std::string invalidItem) {
- cout << "Error: The "<< invalidItem <<" that you have entered is invalid, please try again.\n";
- }
- bool checkIfItemIsvalid(int id, float price) {
- /*
- * id: id to check if it is valid
- * price: price to check if it is valid
- */
- //check id
- if (id <= 0) {
- invalidInput("id");
- return false;
- }
- //check price
- if (price <= 0) {
- invalidInput("price");
- return false;
- }
- return true;
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 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
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement