Help? Stacks Program using Vector Class

Currently I'm working on a program, but I'm not sure how to utilize the variable [top] to be used as an identifier and be displayed and compared to another variable in the program.

I'm not sure if I'm doing it right. Suggestions?

Here's the problem to the program I'm currently working on:
You are to write a program in C++ using the stack data structure as part of your solution. The program should manage cars in a parking lot. This parking lot is unique and must be considered this way. The lot is a long area but has the width of a car. The first car that arrives is the last one to come out since the cars only have one exit as shown in the diagram below. This illustrates the behavior of a stack. When a client arrives, the person in charge gives him a ticket with a ticket # to identify the car in the system. A copy of the ticket is stuck on the car for reference also.

Functionality of program should include:
 The program should allow the user to insert a car into the parking lot.
 The program should allow the user to remove a car from the parking lot given the ticket # assigned to the car. Remember the behavior of a stack- the last one that comes in will be the first to come out. So before you remove, the program must verify that it is the car requested by its owner but at the same time you have to implement some mechanism to not lose the cars taken out temporarily from the parking lot.
 The program should be able to display the number of cars currently in the parking lot to verify space for a client.
 Quit.
Any additional data structures required for the implementation are welcome. The stack must be
implemented using arrays(vectors). Project must be developed in pairs. Program must be bug free.

Here's the program I've got so far:
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
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

class CarStack : public Cars
{
   private:
   
   int maxSize;
   vector <double> stackVect, stackVector;
   int top;
   int carliscense;
   
   public:
   //-------------------------------------------------
   CarStack(int size) : maxSize (size), top(-1) //constructor
   {
       stackVect.reserve(maxSize); //size of vector
   }
   //-------------------------------------------------
   CarStack1 (int license) : carliscense(license)
   {}
   //-------------------------------------------------
   void push(double j) //inserting item on top
   {
       if (isFull())
       {
           cout<<"Database is FULL!. "<<j<<" could not be inserted! \n";
           return;
       }
       stackVect [++top] = j;
       cout<<j<<" has been inserted. \n";
   } //inserting item ending
   //-------------------------------------------------
   double pop()
   {
       int temp;
       if (isEmpty())
       {
           cout<<"Database is EMPTY."<<endl;
           return false;
       }
       
       temp=stackVect[top];
       cout<<temp;
       return stackVect[top];
   }
   //-------------------------------------------------
   double peek()
   {
       int temp;
       if (isEmpty())
       {
           cout<<"Database is EMPTY."<<endl;
           return false;
       }
       
       temp=stackVect[top];
       cout<<temp;
       return stackVect[top];
   }
   //-------------------------------------------------
   bool isEmpty()
   {
       return (top== -1);
   }
   //-------------------------------------------------
   bool isFull()
   {
       return (top == maxSize-1);
   }
   //-------------------------------------------------
   void showmenu()
    {
    cout<<"Please select one of the following to continue \n"
        <<"1. Adding Cars into Database  \n"
        <<"2. Delete a Car from the Database \n"
        <<"3. PEEKING the car that was entered most recently \n"
        <<"4. Quiting the program \n";
    }
   //-------------------------------------------------    
};

int main()
{
     int num = 0; 
     int id = 0;
     int ticketnumber;
     int choice, element, nElem;
     cout<<"Welcome to the Parking Lot Manager Program \n";
     cout<<"Let's begin by entering the number of cars that will be added into the database for today \n"
         <<"    Note if no value is entered, the initial amount of cars that can be stored will be a 100 \n";
         cin>>num;
         if(num == 0)
         {
             cout<<"Let's begin with a 100 slots to store cars \n"<<endl;
             num = 100;
         }
         CarStack theStack(num); //make new stack, size specified by user
         Carreplace theStack1(100); //making new stack in background, size initialized
         theStack.showmenu();
         cin>>choice;
         while (choice != 4)
        {
            switch (choice)
            {
                case 1: //Inserting into STACK
                    cout<<"How much cars are being parked today? \n";
                    cin>>nElem;
                    for (int j=0; j<nElem; j++)
                    {
                        cout<<"Please enter the numbers below one by one \n";
                        cin>>element;
                        theStack.push(element);
                        cout<<"The ticket number for that car is "<<id<<endl;
                        cout<<endl;
                    }
                    theStack.showmenu();
                    cin>>choice;
                break;
            
                case 2: //Deleting elements in the STACK
                    cout<<"Please provide the ticket number of the customer that will be checking out \n";
                    cin>>ticketnumber;
                    //another number that can be implemented here
                    //code snippet suggestions would be of much more help, appreciate it so much
                    theStack.showmenu();
                    cin>>choice;
                break;
            
                case 3: //Peeking first element of STACK
                    cout<<"To view element on top of stack press #1 \n";
                    int input;
                    cin>>input;
                    while (input == 1)
                    {
                        cout<<"The element is ";
                        theStack.peek();
                        cout<<endl;
                        cout<<"To view element on top once more press the #1 \n";
                        cin>>input;
                    }
                    theStack.showmenu();
                    cin>>choice;
                break;    
                
            }
        }
    return 0;     
}
Last edited on
http://www.cplusplus.com/forum/general/233594/
http://www.cplusplus.com/forum/general/233597/

Third thread on same problem (and least own effort shown)?

Do not doublepost.
yes it is, and sorry. deadline is today, still having a little issue understanding the push_back function and how it can be implemented here
Line 6: Cars is undefined. I don't think you need it. All you need to record about a car is its ticket number.

Line 11: vector of double? Why double? You're storing ticket numbers which are ints.
Line 13: the CarStack doesn't have a license. Delete this.

Lines 22 & 23: delete these. they aren't needed.

pop() should return an int. Return -1 to indicate that the lot is empty. Finally, pop() needs to modify top to indicate that there is one less item on the stack.

General comment: don't have push(), pop(), peek() and empty() print anything. The caller should do that. You'll find that sometimes you need to check if the lot is full or empty without printing something.

push() and pop() don't change the size of the stackVect, so it looks like you're assuming that stackVect stays the same size. In that case you want to initialize it with maxsize items rather than reserving space for up to maxsize elements:
1
2
3
4
5
CarStack(int size): maxSize(size), stackVect(maxSize), top(-1)

Option 3 is supposed to display the number of cars currently in the lot.
    {
    }


peek() should return an int.
Line 116: id should be element.

To display the menu use a do/while loop. Something like:
1
2
3
4
5
6
7
do {
    theStack.showMenu();
    cin >> choice;
    switch (choice) {
        ...
    }
while (choice != 4);

This way you don't have to show the menu and prompt inside each case.

I suggest that you create a CarStack::remove() method to remove a car from the stack.
1
2
3
4
5
// Remove the car  with the ticket number. Return true if you removed it and false if it isn't there.
bool remove(int ticketNumber)
{
...
}
Topic archived. No new replies allowed.