[help!]stacks

I'm working on a problem with stacks, to be honest I'm still sorta confused with the topic... so the problem is write a problem to keep track of arriving and departing cars to a garage. the garage has only one entrance/exit so every car will park in order. when a car departs all the cars parked behind it needs to back into the streets to let it out, and the move back into the garage. the garage has a capacity of 6 cars, if a car arrives when it's full the car can't park there, the program will just print out garage's full. the program takes an input file (data4.txt) which has lists of input in the format of code(signaling arrival & departure),license number, and time (in hour and minutes)
inputs look something like this:
A
ABCDE
8 30
A
FGHJK
8 32
D
ABCDE
9 30

for every departure car, the the program should search through the stack for that car, and return "car not found" if the car's not in the garage.

sorry for the wordy description, so here's my code and i can't even get it running, probably cuz i'm not understanding stacks very well, can someone take a look and maybe give me some ideas? thanks!

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
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

struct car
{
	char code;
    string license; 
	int hour;
	int min;
};

class stack
{
  public: 
   static const int CAPACITY = 6;	
   typedef int STACK_TYPE;
   stack();					// constructor
   void push(STACK_TYPE);		// add element to the stack
   STACK_TYPE pop();			// return top element
   bool	is_empty();				// is stack empty?
   bool is_full();

  private:
   int top;
   STACK_TYPE items[CAPACITY];   
};
stack::stack()
{
 top=-1;//nothing on stack initially
}

void stack::push(STACK_TYPE value)
{
 assert(top !=CAPACITY-1);
 top++;
 items[top]=value;
}

stack::STACK_TYPE stack::pop()
{
  STACK_TYPE value;
  assert(top>=0);
  value= items[top];	
  top--;
  return(value);
                
}
bool stack::is_empty()
{
	if (top== -1) return true;
	else return false;
}	

bool stack::is_full()
{
	if (top==CAPACITY-1) return true;
	else return false;
}

void arriving(string &a, int &b, int &c) //prints arriving car's license & time
{
  cout<<"car has been parked in the garage."<<endl;
  cout<<"license plate number: "<<a <<endl;
  cout<<"current time:" << b<<":";
  if (c>=10) cout<<c<<endl;
  else cout<<"0"<<c<<endl;
  cout <<"*********************"<<endl;
  
}


void departing(string &a, int&b, int &c)//prints departing car's license & time
{
  cout<<"License: "<<a<<endl;    
  cout<<"Time of departure: " <<b<<":";
  if (c>=10) cout<<c<<endl;
  else cout<<"0"<<c<<endl;
}

void time_parked(int &a, int &b, int &c, int &d)//prints time parked & charges
{
 int hour_diff;
 int min_diff;
 double charges;
 if (d<b)//if departing minutes is less than arriving minutes
 {
   c--;
 }
 hour_diff = c-a;
 min_diff = d-b;
 charges = 7.5*2*hour_diff;
 if (min_diff>30)
 {
 charges = charges + 7.5*2;
 }
 else if (min_diff>0)
 {
 charges = charges + 7.5;     
 }
 cout<<"Time Parked: "<<hour_diff<<":";
 if (min_diff>=10) cout<<min_diff<<endl;
 else cout<<"0"<<min_diff<<endl;
 cout<<"Total Charges: $"<<charges<<endl;
}

void print_garage();


int main ()
{ 
  stack garage;
  stack street;    
  
  car cars[100]; 
  car temp;
  int new_hour;
  int new_min;
  int x=0;
  
  fstream textfile;
  textfile.open("data4.txt");
  
 textfile>>cars[x].code;
 while(!textfile.eof())
 {
  textfile>> cars[x].license;
 
  if (cars[x].code =="A")
  {
     if (garage.is_full())
     {
     cout<<"Garage is full."<<endl;
     cout<<"License: "<<license<<endl;
     }
     else
     {
       textfile>>cars[x].hour;
       textfile>>cars[x].min;
       arriving(cars[x].license, cars[x].hour, cars[x].min);
       garage.push(cars[x]);
     }      
  }
  if (cars[x].code=="D")
  {
    textfile>>new_hour;
    textfile>>new_min;
    while (!garage.is_empty())
    {
     temp = garage.pop();
     street.push(temp);
     while (temp.license!=cars[x].license)
     {//backing cars from garage to street until finding the matching license car
      if(!garage.is_empty())
      {
       temp = garage.pop();
       street.push(temp);
      }
     } 
     if (temp.license!=cars[x].license)
     {
     cout<<"Car not found"<<endl;
     cout<<"License: "<<cars[x].license;
     }
     else
     {
     departing(cars[x].license, new_hour, new_min);
     time_parked(cars[x].hour, cars[x].min, new_hour, new_min);
     street.pop();
     }
    }
    while(!street.is_empty())
    {
     temp = street.pop();
     garage.push(temp);
    }
    print_garage();        
  }     
 }
 system("pause");
}    


void print_garage()
{
     car temp;
     while (!garage.is_empty())
     {
     temp = garage.pop();
     cout<<temp.license<<endl;
     street.push(temp);
     }
     while(!street.is_empty())
     {
     temp = street.pop();
     garage.push(temp);
     }
}
Last edited on
I modified your code. The modified portions are remarked with ///. Find out what was wrong youself.
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

struct car
{
	char code;
    string license;
	int hour;
	int min;
};

class stack
{
  public:
   static const int CAPACITY = 6;
   typedef car STACK_TYPE;          ///MODIFIED from int to car
   stack();					// constructor
   void push(STACK_TYPE);		// add element to the stack
   STACK_TYPE pop();			// return top element
   bool	is_empty();				// is stack empty?
   bool is_full();

  private:
   int top;
   STACK_TYPE items[CAPACITY];
};
stack::stack()
{
 top=-1;//nothing on stack initially
}

void stack::push(STACK_TYPE value)
{
 assert(top !=CAPACITY-1);
 top++;
 items[top]=value;
}

stack::STACK_TYPE stack::pop()
{
  STACK_TYPE value;
  assert(top>=0);
  value= items[top];
  top--;
  return(value);

}
bool stack::is_empty()
{
	if (top== -1) return true;
	else return false;
}

bool stack::is_full()
{
	if (top==CAPACITY-1) return true;
	else return false;
}

void arriving(string &a, int &b, int &c) //prints arriving car's license & time
{
  cout<<"car has been parked in the garage."<<endl;
  cout<<"license plate number: "<<a <<endl;
  cout<<"current time:" << b<<":";
  if (c>=10) cout<<c<<endl;
  else cout<<"0"<<c<<endl;
  cout <<"*********************"<<endl;

}


void departing(string &a, int&b, int &c)//prints departing car's license & time
{
  cout<<"License: "<<a<<endl;
  cout<<"Time of departure: " <<b<<":";
  if (c>=10) cout<<c<<endl;
  else cout<<"0"<<c<<endl;
}

void time_parked(int &a, int &b, int &c, int &d)//prints time parked & charges
{
 int hour_diff;
 int min_diff;
 double charges;
 if (d<b)//if departing minutes is less than arriving minutes
 {
   c--;
 }
 hour_diff = c-a;
 min_diff = d-b;
 charges = 7.5*2*hour_diff;
 if (min_diff>30)
 {
 charges = charges + 7.5*2;
 }
 else if (min_diff>0)
 {
 charges = charges + 7.5;
 }
 cout<<"Time Parked: "<<hour_diff<<":";
 if (min_diff>=10) cout<<min_diff<<endl;
 else cout<<"0"<<min_diff<<endl;
 cout<<"Total Charges: $"<<charges<<endl;
}

void print_garage();

stack garage;       ///MADE GLOBAL
stack street;       ///MADE GLOBAL

int main ()
{
//  stack garage;
//  stack street;

  car cars[100];
  car temp;
  int new_hour;
  int new_min;
  int x=0;

  fstream textfile;
  textfile.open("data4.txt");

 //textfile>>cars[x].code;
 while(!textfile.eof())
 {
  textfile>>cars[x].code;   ///MOVED DOWN
  textfile>> cars[x].license;

  if (cars[x].code =='A')   ///MODIFIED from "A" to 'A'
  {
     if (garage.is_full())
     {
     cout<<"Garage is full."<<endl;
     cout<<"License: "<<cars[x].license<<endl;  //MODIFIED from license to cars[x].license
     }
     else
     {
       textfile>>cars[x].hour;
       textfile>>cars[x].min;
       arriving(cars[x].license, cars[x].hour, cars[x].min);
       garage.push(cars[x]);
     }
  }
  if (cars[x].code=='D')    ///MODIFIED from "D" to 'D'
  {
    textfile>>new_hour;
    textfile>>new_min;
    while (!garage.is_empty())
    {
     temp = garage.pop();
     street.push(temp);
     while (temp.license!=cars[x].license)
     {//backing cars from garage to street until finding the matching license car
      if(!garage.is_empty())
      {
       temp = garage.pop();
       street.push(temp);
      }
     }
     if (temp.license!=cars[x].license)
     {
     cout<<"Car not found"<<endl;
     cout<<"License: "<<cars[x].license;
     }
     else
     {
     departing(cars[x].license, new_hour, new_min);
     time_parked(cars[x].hour, cars[x].min, new_hour, new_min);
     street.pop();
     }
    }
    while(!street.is_empty())
    {
     temp = street.pop();
     garage.push(temp);
    }
    print_garage();
  }
 }
 system("pause");
}


void print_garage()
{
     car temp;
     while (!garage.is_empty())
     {
     temp = garage.pop();
     cout<<temp.license<<endl;
     street.push(temp);
     }
     while(!street.is_empty())
     {
     temp = street.pop();
     garage.push(temp);
     }
}


i broke the problem down a little, it runs and outputs right but gets stuck after the first car departs, can someone take a look at the code and see what caused the program to pause there? (it printed the time & garage correctly)

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
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>

using namespace std;
struct car
{
 char code;      
 string license;
 int hour;
 int min;       
};

class stack
{
 public:      
   public: 
   static const int CAPACITY = 6;	
   typedef car STACK_TYPE;
   stack();	// constructor
   void push(STACK_TYPE);// add element to the stack
   STACK_TYPE pop();// return top element
   bool	is_empty();	
   bool is_full();
 private:
   int top;
   STACK_TYPE items[CAPACITY];  
};
stack garage;
stack street;

stack::stack()
{
 top=-1;//nothing on stack initially
}

void stack::push(STACK_TYPE value)
{
 assert(top !=CAPACITY-1);
 top++;
 items[top]=value;
}

stack::STACK_TYPE stack::pop()
{
  STACK_TYPE value;
  assert(top>=0);
  value= items[top];	
  top--;
  return(value);                
}

bool stack::is_empty()
{
  if (top== -1) return true;
  else return false;
}	

bool stack::is_full()
{
  if (top==CAPACITY-1) return true;
  else return false;
}

void print_garage()
{
     car temp;
     while (!garage.is_empty())
     {
     temp = garage.pop();     
     street.push(temp);
     }
     while(!street.is_empty())
     {
     temp = street.pop();
     cout<<temp.license<<endl;
     garage.push(temp);
     }
}


int main ()
{
  car cars[100];
  car temp;
  int x=0;
  
  fstream textfile;
  textfile.open("data4.txt");
  textfile>>cars[x].code;
  while(!textfile.eof())
  {                                                
    textfile>> cars[x].license;
           
    cout<<"***********"<<endl;
    if (cars[x].code == 'A')
    {
      textfile>> cars[x].hour;
      textfile>> cars[x].min;               
      if(garage.is_full())
      {
        cout<<"Garage is full"<<endl;
      }                
      else
      {
        garage.push(cars[x]);  
        cout<<"license:"<<cars[x].license<<endl;
        cout<<"time of arrival: ";
        if (cars[x].min<10){
          cout<<cars[x].hour<<":0"<<cars[x].min<<endl;}
        else{
          cout<<cars[x].hour<<":"<<cars[x].min<<endl;}
      }  
    }                     
    else 
    {
      textfile>> new_hour;
      textfile>> new_min; 
      
      while(!garage.is_empty())
      {
        temp = garage.pop();
        street.push(temp);
        while (temp.license!=cars[x].license)
        {//backing cars from garage to street until finding the matching license car
          if(!garage.is_empty())
          {
           temp = garage.pop();
           street.push(temp);
          }           
        }
        
        if (temp.license!=cars[x].license)
        {
          cout<<"Car not found"<<endl;
          cout<<"License: "<<cars[x].license;
        } 
        else
        {
          cout<<"license:"<<cars[x].license<<endl;
          cout<<"time of departure: ";
          if (cars[x].min<10)
            cout<<cars[x].hour<<":0"<<cars[x].min<<endl;
          else
            cout<<cars[x].hour<<":"<<cars[x].min<<endl;
          
          street.pop();
          while(!street.is_empty())
          {
            temp=street.pop();
            garage.push(temp);
          }
          print_garage(); 
        }   
      }
    } 
    cout<<"***********"<<endl;
    x++;
    textfile>> cars[x].code;
  }
  
  system("pause");
}
Topic archived. No new replies allowed.