I posted a thread a bit earlier about a simple error, but this is more of my main issue. I have a struct:

struct Inventory {
    string ItemName;
    int ItemQty;
    float ItemPrice;
};

with the intention of creating an array for storing an inventory array. I can't seem to connect the dots about how to use the Inventory array in functions, ie:

void AddItem() {
    MyInventory[1]->ItemName = "Blah";
    MyInventory[1]->ItemQty = 3;
    MyInventory[1]->ItemPrice = 3.50;
}

The error I get is MyInventory undeclared, but I'm pretty sure that's because I haven't attempted to pass anything, which I'm not sure how to go about this. I'm under a tight deadline so any quick help would be amazing, thanks!

(for bonus points: is there a way to define the array for use with x number of array indexes? I have it set at 1000, but the inventory could be more or less? I want it to be able to expand as needed. This seems like it would be simple.)

Full code:

#include <iostream>
#include <conio.h> // for menu selection input
#include <vector> // arrays

using namespace std;

struct Inventory {
    string ItemName;
    int ItemQty;
    float ItemPrice;
};

int main() {
    Inventory MyInventory[1000]; // enough space for 1000 items, how do we make this virtually infinite?
    
    unsigned key;
    
    // function declarations
    void InventoryOverview();
    void MenuOptions(unsigned &key);
    void PerformFunction(unsigned &key);
    void ProgramTitle();
    void AddItem(Inventory *MyInventory[]);
    //
    
    ProgramTitle();
    InventoryOverview();
    MenuOptions(key);
    PerformFunction(key);
    
    system("PAUSE");
}

// function operations
void ClearScreen() {
}

void ProgramTitle() {
    // just for prettiness
    cout << "                         [Inventory Management System]" << endl << endl;
}

void MenuOptions(unsigned &key) {
    cout << "Select an option:" << endl;
    cout << "[1] Load new inventory from file" << endl;
    cout << "[2] Load additional inventory from file" << endl;
    cout << "[3] Add item to inventory" << endl;
    cout << "[4] Save inventory" << endl;
    cout << "[5] Search inventory" << endl;
    cout << "[6] List inventory" << endl;
    cout << "[0] Exit";
    
    // loop until correct key is inputted
    do {
        key = getch();
    } while(key < 48 || key > 54);
}

void InventoryOverview() {
    cout << "Inventory items: 0" << endl;
    cout << "Inventory value: $0.00" << endl << endl;
}

void NewInventoryFromFile() {
}

void AddInventoryFromFile() {
}

void AddItem() {
    //vector<int> MyInventory;
    //int i = MyInventory.size(); // number of elements in Item
    MyInventory[1]->ItemName = "Blah"; // i will be the next index number
    MyInventory[1]->ItemQty = 3;
    MyInventory[1]->ItemPrice = 3.50;
}

void SaveInventory() {
}

void SearchInventory() {
}

void ListInventory() {
}

void PerformFunction(unsigned &key) {
    if(key == 48) { // 0
        //cout << "Exit";
        exit(1);
    }
    else if(key == 49) { // 1
        //cout << "New inventory from file";
        NewInventoryFromFile();
    }
    else if(key == 50) { // 2
        //cout << "Load additional inventory from file";
        AddInventoryFromFile();
    }
    else if(key == 51) { // 3
        //cout << "Add item to inventory";
        AddItem();
    }
    else if(key == 52) { // 4
        //cout << "Save inventory";
        SaveInventory();
    }
    else if(key == 53) { // 5
        //cout << "Search inventory";
        SearchInventory();
    }
    else if(key == 54) { // 6
        //cout << "List inventory";
        ListInventory();
    }
}

Recommended Answers

All 2 Replies

You need to pass the array as you suggested. Its the same as passing an array of ints, or whatever, its just a different type. For example :

int search(Inventory* items, int size){
 /*code to search */
}
void print(Iventory* items, int size){
  /* code to print */
}

So essentially, you need to pass in the inventory.


As for your bonus question, you can use std::vector<Inventory> items;
Google on how to use std::vector.

commented: Nice +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.