0

I have "items" in an "inventory" array, the inventory array is a struct composed of several different parts however I want to be able to search for a certain part of the array by using the item name alone (so I can make functions to delete, purchase, etc.) I was wondering how I would go about doing so and if it would instead be easier to convert the struct to a class instead if need be. Keep in mind a good amount of it is still a work in progress, I'm specifically aiming for the removeItem function currently however. Thanks in advance for any and all help!

my code is here:

#include "integerstore.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void removeItem(Item);
int buyItem(Item, int);
int sellItem(Item, int);
void stockReport(Item);

struct Item
{
string name;
float cost;
float price;
int quantity;
};

int main()
{
Item inventory[100];
int total = 0;
string cmd;

cout << "Welcome to inventory management. To add a new inventory item type add, to remove and item from inventory type remove, to add to inventory quantity type buy, to mark a sold item type sell, to see the current inventory report type report and to end the program type stop" << endl;

do
{
    cin >> cmd;

    if (cmd == "add") 
    {
        cout << "Enter the new item's name";
        cin >> inventory[i].name;
        cout << endl;
        cout << "Enter the new item's cost";
        cin >> inventory[i].cost;
        if (inventory[i].cost <= 100.00)
        {
            break;
        }
        else
        {
            cout << "Cost of item may not exceed 100.00, please reenter.";
            cin.clear();
        }
        cout << endl;
        cout << "Enter the new item's selling price";
        cin >> inventory[i].price;
        if (inventory[i].price <= 100.00)
        {
            break;
        }
        else
        {
            cout << "Selling price may not exceed 100.00, please reenter.";
            cin.clear();
        }
        cout << endl;
        i++;
    }
    else if (cmd == "remove") 
        removeItem(inventory[100]);

    else if (cmd == "buy") 
        buyItem(inventory[100], total);

    else if (cmd == "sell") 
        sellItem(inventory[100]);

    else if (cmd == "report") 
        stockReport(inventory[100]);

    else if (cmd != "stop") 
        cout << "invalid";
}
while (cmd != "stop");

system ("pause");
return 0;
}


int buyItem(Item w, int w2)
{
string itemName;
cout << "Enter the item name of which you wish to add more stock to" << endl;
cin >> itemName;


}

int sellItem(Item x, int x2)
{

}

void removeItem (Item y)
{
string itemName;
cout << "Enter the item name you wish to remove from the inventory" << endl;
cin >> itemName;
}


void stockReport(Item z)
{

}
1
  • Instead of array, why not use map with string name as key? Commented Sep 19, 2013 at 6:17

1 Answer 1

2

You can use a map<string, Item> (you might want to remove name from the Item struct).

Then you can use it for instance like this:

map<string, Item> inventory;
// Add
inventory["stuff"].cost = 12;
// ...
// Get
string name;
cin >> name;
Item myItem = inventory[name];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.