-1

Here is part of the code:

class Inventory {
    public:
        void PrintInventory();
        void AddItemToInventory();
        void UpdateItemQtyInInventory();
        void RemoveItemFromInventory();
    private:
        vector<Item*> inventory;
};

void Inventory::PrintInventory() {
    unsigned int i = 0;
    if (inventory.size() == 0) {
        cout << "No items to print." << endl;
    } else {
        for (i=0; i < inventory.size(); ++i) {
            cout << i << " - ";
            inventory.at(i)->Print();
        }
    }
    return;
}

void Inventory::AddItemToInventory() {
    string usrInptName = "";
    string usrInptQntyStr = "";
    string usrInptPriceStr = "";
    string usrInptOptn = "default";
    istringstream inSS;
    int usrInptQnty = 0;
    int usrInptPrice = 0;
    string usrInptOther = "";
    bool valid = false;

    while (valid == false) {
        cout << "Enter (b)ook or (p)roduce: ";
        getline(cin, usrInptOptn);

        if (usrInptOptn.size() == 0) {
                continue;
        } else if (usrInptOptn.at(0) == 'p') {
            Produce* prdct;

            cout << "Enter name of new produce: ";
            getline(cin, usrInptName);

            cout << "Enter the price per item: $";
            getline(cin, usrInptPriceStr);
            inSS.str(usrInptPriceStr);
            inSS >> usrInptPrice;
            inSS.clear();

            cout << "Enter quantity: ";
            getline(cin, usrInptQntyStr);
            inSS.str(usrInptQntyStr);
            inSS >> usrInptQnty;
            inSS.clear();

            cout << "Enter expiration date: ";
            getline(cin, usrInptOther);

            prdct = new Produce;
            prdct->SetName(usrInptName);
            prdct->SetPrice(usrInptPrice);
            prdct->SetQuantity(usrInptQnty);
            prdct->SetExpiration(usrInptOther);

            inventory.push_back(prdct);
            valid = true;

        } else if (usrInptOptn.at(0) == 'b') {
            Book* prdct;

            cout << "Enter name of new book: ";
            getline(cin, usrInptName);

            cout << "Enter the price per item: $";
            getline(cin, usrInptPriceStr);
            inSS.str(usrInptPriceStr);
            inSS >> usrInptPrice;
            inSS.clear();

            cout << "Enter quantity: ";
            getline(cin, usrInptQntyStr);
            inSS.str(usrInptQntyStr);
            inSS >> usrInptQnty;
            inSS.clear();

            cout << "Enter Author: ";
            getline(cin, usrInptOther);

            prdct = new Book;
            prdct->SetName(usrInptName);
            prdct->SetPrice(usrInptPrice);
            prdct->SetQuantity(usrInptQnty);
            prdct->SetAuthor(usrInptOther);

            inventory.push_back(prdct);
            valid = true;
        } else {
            cout << "Invalid choice\n";
        }

    }

    return;
}

Inventory* inventory;

int main() {
    string usrInptOptn = "default";

    while (true) {
        // Get user choice
        cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
        getline(cin, usrInptOptn);

        // Process user choice
        if (usrInptOptn.size() == 0) {
            continue;
        } else if (usrInptOptn.at(0) == 'p') {
            inventory->PrintInventory();
        } else if (usrInptOptn.at(0) == 'a') {
            inventory->AddItemToInventory();
        } else if (usrInptOptn.at(0) == 'u') {
            inventory->UpdateItemQtyInInventory();
        } else if (usrInptOptn.at(0) == 'r') {
            inventory->RemoveItemFromInventory();
        } else if (usrInptOptn.at(0) == 'q') {
            cout << "\nGood bye." << endl;
            break;
        }

    }

    return 0;
}

The program crashes when it is running the functions. I don't have the slightest idea why. The AddItemtoIventory function runs through it's entirety before the program crashes while the other one ,and the others, just crashes.

Eegxeta
  • 95
  • 3
  • 5
  • 2
    What about using the debugger to step through your code, and ask a more specific question? – πάντα ῥεῖ Nov 21 '14 at 18:28
  • 1
    `Inventory* inventory;` <- this. This is uninitialized and bad. 1. You don't need a global here. 2. You don't need a pointer here. – crashmstr Nov 21 '14 at 18:28
  • It would be helpful if you could post crash logs related to this issue – Paul Renton Nov 21 '14 at 18:28
  • You call through a `NULL`-pointer and wonder why all hell breaks loose? Isn't it obvious? – Deduplicator Nov 21 '14 at 18:31
  • Include your Inventory pointer inside main and initialise it by using new and calling the constructor for Inventory class. You are invoking functions on a pointer that points to nothing. – itachi Nov 21 '14 at 18:33
  • So "my understanding of C++ is feeble at best, BTW I am qualified to know that is not a dupe" interesting – Lightness Races in Orbit Nov 21 '14 at 18:35
  • I don't understand pointers. I have no idea what a null instance is or what it has to do with this. I thought Inventory* inventory; was initializing the object. To sum everything up I don't know what the hell I'm doing, ok. So no this isn't a dupe because that other question doesn't help me at all. – Eegxeta Nov 21 '14 at 18:50
  • What is undefined behavior? – Eegxeta Nov 21 '14 at 18:57

1 Answers1

-1

You do not need a global variable here. Just create an inventory object within the scope of main

int main() {
    Inventory inv;
    // Rest of your code below

    return 0;
}

Or, alternatively you can still use a pointer and use new.

int main() {
    Inventory* inv = new Inventory;
    // Rest of your code below

    return 0;
}
Paul Renton
  • 2,652
  • 6
  • 25
  • 38