Store backroom

Hello guys i am trying to build a "grocery backroom" here is where i want the user to enter an item and store it in one of the aisles in the back. So for instance if the user picks to store the item in aisle one in the back then store it in there. I know i have to use vectors and was wondering if it is best to use stacks of the list but having trouble so far. This is what i have so far and want it to work as shown below am i approaching it the right way.





aisle 1---
popcorn
chicken treats
cereal


aisle 2---
noodles
pizza
oions
tomatoes

aisle 3--



------------- menu list -------------
cout << "1: print the stack
cout << "2: add item
cout << "3: delete item
0: wish to exit




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
#include <iostream>
#include <list>
#include <vector>

using namespace std;

class backroom{
    
    private:
        //the name of the product
        vector<string> NAME;
        
        //the amount of the items held in the backroom
        vector<int> AMOUNT;
        
        //the amount of aisles in the back room
        vector<int> BACKROOM;
            
    public:
    
    
        // not sure if i need this part
        
        //reads the list that they enter
        list<string> readstack();
        //adds to the stack
        list<string> addstack();
        // delete from the stack
        list<string> deletestack();
        
        
        //getters and setters
        void setName(vector<string> name){
            NAME = name;
        }
        
        string getName(){
            return NAME;
        }
        
        void setAmount(vector<int> amount){
            AMOUNT = amount;            
        }
        
        int getAmount(){
            return AMOUNT;
        }
        
        void setBackroom(vector<int> backroom){
            BACKROOM = backroom;
        }
        
        int getBackroom(){
            return BACKROOM;
        }
        
};


    //print the menu
    void printmenu();
    
    // adds to the stack of given 'aisle'
    void additem(string,int);

    // prints the backroom to check if there is any item in the back
    void printitem(int);
    
    //removes the item in the stack
    void deleteitem(string,int);

int main(){
    
    int choice;
    
    printmenu();
    
    
    // invalid input from the user 
    try{
        cout << "enter the choice you woild like to use" << endl;
        cin >>  choice;
        
	// error catch invalid input 
	// add do while loop until valid
    }catch(Exception e){
     	cout << "system error " << endl;   
    }
    
    
    
    do{
        
        // user choice to enter for the menu;
        switch(choice){
            
            
            case 1:{
                    // creates the varible inside the case 1 
                    int location;
                
                    cout << "print the stack \n" << endl;
                    cout << "witch stack would you like to print out" << endl;
                    cin >> location;
		    printitem(location);
                break;
            }
                
	    case 2:{
                
                string product;
                int location;
                
                    cout << "add item \n" << endl;
                    cout << "which item you wish to add";
                    getline(cin, product);
                    cout << "witch location will you like to add to" << endl;
                    cin >> location;
                    additem(product, location);
                break;
            }
                
            case 3:{ 
                
                    string product;
                    int location;
            
                    cout << "delete item \n" << endl;
                    cout << "which item you wish to delete" << endl;
                    getline(cin, product);
                    cout << "from witch location will you like to deltele from" << endl;
                    cin >> location;
                    deleteitem(product, location);
                break;
            }
                
            case 0: cout << "exit the server \n" << endl;
                    //system(exit());
                break;
            default:
                cout << "invalid choice" << endl;
        }
    
        
    } while(choice != 0);
    
    
    
    
    return 0;
}




// menu print out
 void printmenu(){
        
        cout << "------------- menu list ------------- \n" << endl;
        cout << "1: print the stack" << endl;
        cout << "2: add item " << endl;
        cout << "3: delete item" << endl;
        cout << "0: wish to exit" << endl;
        cout << "------------------------------------- \n" << endl;
        
    }
Last edited on
Warehouse might be a better name for the model:

Item - has - product_name
Aisle - has - aisle_number, Item's
Warehouse - has - Aisle's

So, you have at least 2, if not 3, separate classes depending on whether there are many warehouses or just one.
Hello rezy3312,

I put your code in MSVS 2017 and when compiled received 11 errors.

I lost 2 errors when I changed the beginning.
1
2
3
4
5
6
#include <iostream>
#include <list>
#include <string>  // <--- This is missing. Added.
#include <vector>

using namespace std;  // <--- Best not to use. 

You need to include the <string> header file so that "std::cout" knows how to process a string variable and "std::getline' is defined in the <string> header file and with the header file "std::getline" is undefined.

Next this was flagged:
1
2
3
4
string getName()
{
    return NAME;
}

It looks like you want to return a single name, but "NAME" is defined as a vector, so you are trying to return a vector as a single string. This does not work.

Same is true for "getAmount" and "getBackroom".

In your private variables using all capital letters for the variable names tends to imply that they are defined as constants that can not be changed. Lower case letters for regular variable is more common.

Also Starting a class or a struct with a capital letter is more common.

These are just suggestions you can do what you want, but others might complain.

I do not understand what you are using the try/catch for in "main", but the catch part parameter is a problem. Not something I use often, so I will have to look that one up.

That is just a start that needs fixed before I can see what the program is doing.

One of the first things I noticed is:
1
2
3
4
5
6
7
8
9
10
11
 void printmenu()
{
        
        cout << "------------- menu list ------------- \n" << endl;
        cout << "1: print the stack" << endl;
        cout << "2: add item " << endl;
        cout << "3: delete item" << endl;
        cout << "0: wish to exit" << endl;
        cout << "------------------------------------- \n" << endl;
        
    }

You do not need a "cout" and "endl" for each line. Also prefer to use the new line (\n) over "endl".

Consider this. It is much easier to work with:
1
2
3
4
5
6
7
8
9
10
11
void printmenu()
{
    cout <<
        " ------------- menu list ------------- \n"  // <--- The space at the end does nothing.
                                                     // It may take up room on the screen, but you will never know that it is there.
        " 1: print the stack\n"
        " 2: add item\n"
        " 3: delete item\n"
        " 0: wish to exit\n"
        " ------------------------------------- \n";
}

Each line of quoted text may seem separate, but it is actually considered just 1 string.

Andy
Hello rezy3312,

I cleaned up the code a bit. See what you think and read the comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <list>
#include <string>  // <--- This is missing. Added.
#include <vector>

using namespace std;  // <--- Best not to use.

class backroom
{
    private:  // <--- After the opening { this is private by default. "private:" is not necessary, but OK if you leave.
        //the name of the product
        vector<string> NAME;

        //the amount of the items held in the backroom
        vector<int> AMOUNT;

        //the amount of aisles in the back room
        vector<int> BACKROOM;

    public:
        // not sure if i need this part
        // <--- Since you do not have any functions yet. Save them till later, but they may not be needed in the first place.

        //reads the list that they enter
        //list<string> readstack();
        //adds to the stack
        //list<string> addstack();
        // delete from the stack
        //list<string> deletestack();

        //getters and setters
        void setName(vector<string> name)
        {
            NAME = name;
        }

        //string getName()  // <--- Trying to return a vector as a single string.
        //{
        //    return NAME;
        //}

        void setAmount(vector<int> amount)
        {
            AMOUNT = amount;
        }

        //int getAmount()
        //{
        //    return AMOUNT;
        //}

        void setBackroom(vector<int> backroom)
        {
            BACKROOM = backroom;
        }

        //int getBackroom()
        //{
        //    return BACKROOM;
        //}
};

//print the menu
void printmenu();

// adds to the stack of given 'aisle'
void additem(string, int);

// prints the backroom to check if there is any item in the back
void printitem(int);

//removes the item in the stack
void deleteitem(string, int);

int main()
{
    constexpr int MINVAL{ 1 }, MAXVAL{ 4 };
    int choice;

    printmenu();

    // invalid input from the user 
    //try
    //{
    //    cout << "enter the choice you woild like to use" << endl;
    //    cin >> choice;

    //    // error catch invalid input 
    //    // add do while loop until valid
    //}
    //catch (exception& e)
    //{
    //    std::cerr << "     " << e.what() << '\n';

    //    std::cout << "system error " << endl;
    //}
    //catch (...)
    //{
    //    std::cout << "system error " << endl;
    //}

    while (std::cout << "enter the choice you woild like to use: " && !(std::cin >> choice) || choice < MINVAL || choice > MAXVAL)
    {
        if (!std::cin)
        {
            std::cerr << "\n     Invalid Input! Must be a number.\n\n";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }
        else if (choice < MINVAL || choice > MAXVAL)
        {
            std::cerr << "\n     Invalid Choice! Try again.\n\n";
        }
    }

    do
    {
        // user choice to enter for the menu;
        switch (choice)
        {
            case 1:
            {
                // creates the variable inside the case 1 
                int location;

                cout << "print the stack \n" << endl;
                cout << "witch stack would you like to print out" << endl;
                cin >> location;
                //printitem(location);
                break;
            }

            case 2:
            {
                string product;
                int location;

                cout << "add item \n" << endl;
                cout << "which item you wish to add";
                getline(cin, product);
                cout << "witch location will you like to add to" << endl;
                cin >> location;
                //additem(product, location);
                break;
            }

            case 3:
            {
                string product;
                int location;

                cout << "delete item \n" << endl;
                cout << "which item you wish to delete" << endl;
                getline(cin, product);
                cout << "from witch location will you like to deltele from" << endl;
                cin >> location;
                //deleteitem(product, location);
                break;
            }

            case 4:
                cout << "\n     Exit the server \n" << endl;
                //system(exit());  // <--- Do not use "exit". But this line is not needed in the first place.
                break;

            default:  // <--- The while loop takes care of this. This will not be used.
                cout << "invalid choice" << endl;
        }
    } while (choice != 4);

    return 0;  // <--- Not required, but makes a good break point.
}

// menu print out
void printmenu()
{
    //cout << "------------- menu list ------------- \n" << endl;
    //cout << "1: print the stack" << endl;
    //cout << "2: add item " << endl;
    //cout << "3: delete item" << endl;
    //cout << "0: wish to exit" << endl;
    //cout << "------------------------------------- \n" << endl;

    cout <<
        " ------------- menu list ------------- \n"  // <--- The space at the end does nothing. It may take up room on the screen, but you will never know that it is there.
        " 1: print the stack\n"
        " 2: add item\n"
        " 3: delete item\n"
        " 4: wish to exit\n"
        " ------------------------------------- \n";
}

Some parts I had to comment out because they do not exist yet or need fixed.

I tried to figure out the "try/catch", but that would need some more work. The while loop should do a better job for you.

Andy
@rezy...
Menu's aren't all that important because there might be many user interfaces (console, GUI, tablet etc) to interact with the same data model which as I said earlier is worth reconsidering and simplifying along the lines of normal O-O practice. What follows is a rough first pass. (it's obviously not menu driven because it's too early, especially if you are still grappling with where and how your data is stored/retrieved/updated as you mention at the start.)

Build and test the required functionality as you go and build up the required operations step by step. The menu and all the pretty typography follows as the 'last' step and falls into place easily.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Item
{
private:
    int ref_no;
    string name;
public:
    Item(int ref, string aName)
    {
        ref_no = ref;
        name = aName;
    }

    int getNo(){ return ref_no;}
    string getName(){return name;}
};



class Aisle
{
private:
    int ref_no;
    vector<Item> content;

public:

    Aisle(int ref)
    {
        ref_no = ref;
    }

    void addItem(Item item)
    {
        content.push_back(item);
    }

    void listItems()
    {
        cout << "Aisle no. " << ref_no << '\n';
        for(auto i:content)
        {
            cout << i.getNo() << " - " << i.getName() << '\n';
        }
        cout << '\n';
    }
};

int main()
{
    // MAKE SOME ITEMS
    Item i1(10, "noodles");
    Item i2(873, "popcorn");
    Item i3(12, "pizza");
    Item i4(567, "onions");
    Item i5(908, "tomatoes");

    // MAKE SOME AISLES
    Aisle a1(111);
    Aisle a2(12);
    Aisle a3(87);

    // PLACE ITEMS IN AISLES
    a1.addItem(i1);
    a1.addItem(i2);

    a2.addItem(i3);

    a3.addItem(i5);
    a3.addItem(i4);

    // THIS IS WHERE YOU HAVE A vector<Aisle> OR HAVE A CLASS
    vector<Aisle> warehouse;
    warehouse.push_back(a1);
    warehouse.push_back(a2);
    warehouse.push_back(a3);

    // LIST OUT AISLE CONTENTS INDIVIDUALLY

    cout << "LIST OF AISLE CONTENTS INDIVIDUALLY\n";
    a1.listItems();
    a2.listItems();
    a3.listItems();
    cout << '\n';

    // LIST OUT EVERYTHING IN THE warehouse

    cout << "LIST OF EVERYTHING IN THE warehouse\n";
    for(auto i: warehouse)
    {
        i.listItems();
    }

    return 0;
}



LIST OF AISLE CONTENTS INDIVIDUALLY
Aisle no. 111
10 - noodles
873 - popcorn

Aisle no. 12
12 - pizza

Aisle no. 87
908 - tomatoes
567 - onions


LIST OF EVERYTHING IN THE warehouse
Aisle no. 111
10 - noodles
873 - popcorn

Aisle no. 12
12 - pizza

Aisle no. 87
908 - tomatoes
567 - onions

Program ended with exit code: 0
Hello rezy3312,

When I got the program to run I tried this:

 ------------- menu list -------------
 1: print the stack
 2: add item
 3: delete item
 4: wish to exit
 -------------------------------------
 Enter the choice you woild like to use: 1

 Print the stack
 Witch stack would you like to print out


This is a good start, but how would I know what is available? Does Print the stack mean that something is missing? Like a list of the stacks.

I would suggest that you concentrate on 1 part at a time. Such as taking the first menu choice and making it work.

One problem here is that when the program first runs there is no information in your class to work with. You may want to read a file to set up the program before you display the menu.

Andy
Topic archived. No new replies allowed.