Creating a stack with Inventory class objects, having some problems

Hi, so I've accomplished the program as a queue but I am having trouble turning into a stack.

[Directions](https://bpaste.net/raw/cb4cbf7070d0 ) of the queue example. Same idea for the stack.

[Here](https://bpaste.net/raw/e5e8dc7f98e2 ) is the queue problem that I solved no problem. Looking over the code, it seems to be the same with the code for the stack implementation I have below, but it obviously isn't working properly as it returns what seems to be one less item than it should be in display().



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
    #include <iostream>
    #include <string>
    #include <stack>
    
    using namespace std;
    
    class Inventory
    {
    private:
        long serialNumber;
        int lotNumber;
        string manufactDate;
    public:
        Inventory() = default;
        Inventory(long s, int l, string d) : serialNumber(s), lotNumber(l), manufactDate(d) {}
        void setSerial(long s);
        long getSerial() { return serialNumber; }
        void setLot(int l);
        int getLot() { return lotNumber; }
        void setDate(string d);
        string getDate() { return manufactDate; }
    };
    
    void Inventory::setSerial(long s)
    {
        serialNumber = s;
    }
    
    void Inventory::setLot(int l)
    {
        lotNumber = l;
    }
    
    void Inventory::setDate(string d)
    {
        manufactDate = d;
    }
    
    void addItem(stack<Inventory> &invStack) // get data, store into object, push onto stack
    {
        long serial;
        string date;
        int lotNumber;
        
        cout << "Enter the following information:" << endl <<
                " - Serial Number: ";
        cin >> serial;
        cout << " - Lot Number: ";
        cin >> lotNumber;
        cout << " - Manufacturing Date: ";
        cin >> date;
        cin.clear();
        cin.ignore();
        
        Inventory obj(serial, lotNumber, date);
        invStack.push(obj);
    }
    
    void takeItem(stack<Inventory> &invStack) // pop the top item
    {
        if (invStack.size() > 0)
            invStack.pop();
    }
    
    void display(stack<Inventory> &invStack) // print out contents of member values of all objects
    {
        for (int i = 0; i < invStack.size(); ++i)
        {
            Inventory obj = invStack.top();
            
            cout << "Item #" << i + 1 << endl <<
                    "Serial Number: " << obj.getSerial() << endl <<
                    "Lot Number: " << obj.getLot() << endl <<
                    "Manufacturing Date: " << obj.getDate() << endl << endl;
            
            invStack.pop();
        }
    }
    
    int main()
    {
        int choice;
        
        stack<Inventory> invStack;
        
        do {
            cout << "Options" << endl <<
                    "-------" << endl <<
                    "1. Add part" << endl <<
                    "2. Take part" << endl <<
                    "3. Quit" << endl <<
                    "Would you like to enter an item into inventory?" << endl;
            cout << "Choice: ";
            cin >> choice;
            
            if (choice == 1)
                addItem(invStack);
            else if (choice == 2)
                takeItem(invStack);
        } while (choice != 3);
        
        display(invStack);
    }

Last edited on
In line 67 you have:

1
2
3
4
5
for (int i = 0; i < invStack.size(); ++i) {
.
.
.
}

However, each time you pop from the stack you're decreasing invStack.size();
So let's say we have a stack with:
1 2 3 4
you pop 4 off i = 1, size = 3;
1 2 3
you pop 3 off i = 2, size = 2;
You're done. and you miss the last two.

There's two ways to fix this:
1
2
3
4
5
6
int end = invStack.size();
for(int i = 0; i < end; ++i) {
.
.
.
}


or

1
2
3
4
5
6
7
int i = 0; // For outputing the Item#;
while(!invStack.empty()) {
.
.
.
++i;
}
Last edited on
Topic archived. No new replies allowed.