1

So for the most part I understand what I did wrong, the issue is I don't know how to fix it.

Goal: This is a store management system that must include a menu and inventory management functions that can be manipulated. To do this I used arrays to add the store's items, their descriptions, and their quantities. All the arrays are partially filled to the third element and have a max value of ten elements.

Issue: When the program is run the first time it works and the user can see their inventory, description and quantity. However when they exit to the menu and come BACK to inventory, everything past the third element is cleared. This is due to the declarations initializing the arrays past the third element to 0. How do I fix this to guarantee the user has their inventory saved and can view it upon return?

#include <iostream>       
#include <vector>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>


using namespace std;

//declarations
int mainMenu(); //done
void inventoryMgmt();
void addInv(); //working on
void invView(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray);
void editor(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray);
void customerReciept();  //done
void calculatePrice(); //done
void exit(); //done

const double massTax = 0.0625;

//main
int main() {
    int choice;
    bool repeat = true;

    while (repeat = true) {
        choice = mainMenu();

        switch (choice) {
        case 1:
            customerReciept();
            break;
        case 2:
            inventoryMgmt();
            break;
            //case 3:
            //itemSearcher();
            //break;
        case 4:
            exit();
            repeat = false;
            break;
        }
    }

    return 0;
}

//main menu function ***done
int mainMenu() {
    cout << "\n\n\t\t\t\t Welcome to the Massasoit Store Management System " << endl;
    cout << "\t\t\t\t ________________________________________________ \n\n";
    cout << "\t\t\t\t\t Main Menu: " << endl;
    cout << "\t\t\t\t\t\t 1. Customer Reciept" << endl;
    cout << "\t\t\t\t\t\t 2. Inventory Management" << endl;
    cout << "\t\t\t\t\t\t 3. Item Search" << endl;
    cout << "\t\t\t\t\t\t 4. Exit" << endl << endl;
    int choice;
    cout << "\t\t\t\t\t\t Where do you need to go?: ";
    cin >> choice;

    while (choice < 1 || choice > 4) {
        cout << "\t\t\t\t\t Incorrect Selection Please Select Again: ";
        cin >> choice;
    }
    return choice;
}

//customer reciept function  **done
void customerReciept() {
    cout << "\n\n\t\t\t\t Welcome to the Massasoit Store: Customer Reciept " << endl;
    cout << "\t\t\t\t ________________________________________________ \n\n";
    cout << "\t\t\t\t\t Receipt Menu: " << endl;
    cout << "\t\t\t\t\t\t 1. Calculate Receipt" << endl;
    cout << "\t\t\t\t\t\t 2. Return to Main" << endl;
    int recieptChoice;
    cout << "\t\t\t\t\t\t Where do you need to go?: ";
    cin >> recieptChoice;
    while (recieptChoice < 1 || recieptChoice > 2) {
        cout << "Invalid Selection Please Choose Again: ";
        cin >> recieptChoice;
    }
    if (recieptChoice == 1) {
        calculatePrice();
    }
}

void calculatePrice() {
    double cost;
    double taxAmount;
    int numOfItems;
    double finalCost = 0;
    char tax;
    int i;
    cout << "\n\n\t\t\t\t Welcome to the Massasoit Store: Customer Reciept " << endl;
    cout << "\t\t\t\t ________________________________________________ \n\n";
    cout << "How many items were purchased?: ";
    cin >> numOfItems;
    for (i = 0; i < numOfItems; i++) {
        cout << "What did item " << i + 1 << " cost? $";
        cin >> cost;
        cout << "Is this item taxable? (y/n):";
        cin >> tax;
        if (tax == 'y' || tax == 'Y') {

            taxAmount = (cost * massTax);
            cost = taxAmount + cost;
            finalCost = cost + finalCost;
        }
        else {
            finalCost = cost + finalCost;
        }
    }
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "This customer's total is $" << finalCost << endl;
}

void inventoryMgmt() {

    int invChoice;
    cout << "\n\n\t\t\t\t\t Welcome to Inventory Management " << endl;
    cout << "\t\t\t\t ______________________________________________ \n\n";
    cout << "\t\t\t\t\t Inventory Settings: " << endl;
    cout << "\t\t\t\t\t\t 1. Add/View & Edit/Delete Inventory" << endl;
    cout << "\t\t\t\t\t\t 2. Return to Main" << endl;
    cout << "\t\t\t\t\t\t Where do you need to go?: ";
    cin >> invChoice;
    cout << endl << endl;
    while (invChoice < 1 || invChoice > 2) {
        cout << "Invalid Selection Please Choose Again: ";
        cin >> invChoice;
    }
    if (invChoice == 1) {

        addInv();
    }
    if (invChoice == 2) {
        //edit/delete();
    }
}

void addInv() {
    //so when this is called, everything is initialized to 0 which makes it wipe the memory
    //when the user comes back to it from the menu.

    const int description = 20; //this allows a short description for each item
    const int counter = 10; //slots of inventory allowed
    int quantity[10] = {10, 15, 45};
    string itemArray[counter] = { "Hot Drinks", "Cold Drinks", "Books"};
    string descriptionArray[description] = { "Coffee, Tea etc", "Water, juice, etc", "Texts, notebook, etc"};
    char addChoice;
    int destination;
    cout << "\t\t\t\t\t\t 1. Add/View" << endl;
    cout << "\t\t\t\t\t\t 2. Edit/Delete" << endl;
    cout << "\t\t\t\t\t\t 3. Exit to Main" << endl;
    cout << "\t\t\t\t\t\t Where would you like to go?: " << endl;
    cin >> destination;

    while (destination < 1 || destination > 3) {
        cout << "Invalid Selection, Please Select Again: ";
        cin >> destination;
    }

    if (destination == 1) {
        cout << "Would you like to add an item? (y/n): ";
        cin >> addChoice;
        int i = 3;      //these two are initialized to three to ensure that the store 
        int ii = 3;     //will always have hot drinks, cold drinks and books as options 
        while (addChoice == 'y' && i < counter) {

            cout << "What would you like to add?: ";
            cin >> itemArray[i];
            cout << "You have added \"" << itemArray[i] << "\" to the inventory\n";
            cout << "Please Provide a Brief Description: ";
            cin.ignore();
            getline(cin, descriptionArray[ii]);
            cout << "You've described \"" << itemArray[i] << "\" as  \"" << descriptionArray[i] << "\"" << endl;
            cout << "What is the quantity of " << itemArray[i] << " in stock?";
            cin >> quantity[i];
            cout << "Would you like to add another item?";
            cin >> addChoice;
            i++;
            ii++;
            if (i > counter) {
                cout << "**INVENTORY LIMIT REACHED*** ";
            }
        }
        invView(itemArray, 10, descriptionArray, 10, quantity, 10); //working on this. //so use this for view, edit, and delete.
    }
}

void invView(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray) {   //add quantity

    int i = 0;
    int ii = 0;
    int iii = 0;
    cout << "Your inventory consists of: ";
    while (i < sizeofArray && x[i] != "" && ii < secondArray && y[i] != "" && iii < thirdArray) {
        cout << x[i] << " - " << y[i] << " | Quantity: " << z[i] << endl;
        i++;
        ii++;
    }
}

void editor(string x[], int sizeofArray, string y[], int secondArray, int z[], int thirdArray) {
    cout << "Which item would you like to edit?";
}

void exit() {
    cout << "\n\n\t\t\t\t Welcome to the Massasoit Store Management System " << endl;
    cout << "\t\t\t\t ________________________________________________ \n\n";
    cout << "\t\t\t\t Thank you for using the Massasoit Store Management System" << endl;
    exit(1);
}

I included the entire code in case it was needed. The issues are centered around the inventoryMgmt() and addInv() functions. Thanks in advance for any help!

Barmar
  • 741,623
  • 53
  • 500
  • 612
VivMike
  • 47
  • 3
  • 5
    It's tough to work through a chunk of code that big - please try to create a [mcve]... – hlt May 09 '18 at 20:04

1 Answers1

0

In theaddInv() all the arrays are local. Once that function is done executing, all local variables are freed up thus resulting in your deletion. Try place them outside your addInv() function:

    int main(){
        int quantity[10] = {10, 15, 45};
        string itemArray[counter] = { "..."};
        string descriptionArray[description] = { "..."};

    void addInv() {...}
    }
GKE
  • 960
  • 8
  • 21