[help]stacks

i'm working on a problem with stacks, that's supposed to keep track of incoming and outgoing cars of a garage.
the program takes in an input file, and is supposed to read in every arriving and departing car and prints out arriving and departing time and parking cost.
my code runs correctly up until the first departing car, it prints up then and prints cars in the garage but then the code just got stuck there and won't run any further, can someone take a look and give me a hint what made it stuck? 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
202
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>

using namespace std;
struct car
{
 char code; //in 'A' or 'D' for arriving or departing     
 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;
     cout<<"Cars parked in the garage: "<<endl;
     while (!garage.is_empty())
     {
     temp = garage.pop();     
     street.push(temp);
     }
     while(!street.is_empty())
     {
     temp = street.pop();
     cout<<temp.license<<endl;
     garage.push(temp);
     }
}

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
 {
   hour_diff = c-a-1;
   min_diff = d-b+60;
 }
 else
 {
 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;
}


int main ()
{
  car cars[100];
  car temp;
  int a_hour;
  int a_min;
  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;
        cout<<"license:"<<cars[x].license<<endl;
      }                
      else
      {
        cout<<"The car has been parked in the garage. "<<endl;    
        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>> cars[x].hour;
      textfile>> cars[x].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;
          time_parked(temp.hour, temp.min, cars[x].hour, cars[x].min);
          street.pop();
          while(!street.is_empty())
          {
            temp=street.pop();
            garage.push(temp);
          }
          cout<<endl;
          print_garage(); 
        }   
      }
    } 
    cout<<"************************************************"<<endl;
    x++;
    textfile>> cars[x].code;
  }
  
  system("pause");
}
Last edited on
Topic archived. No new replies allowed.