The program reads a text file with the contents
A DPX
A QRT
Pushes DPX and QRT on stack. But while displaying stack{void show()}, it does not display stack[0]. No idea Why!? It is being pushed onto the stack. element stack[0] diplayed in push() but not in show()
const int MAX = 10 ;
struct car
{
char * lPlate;
int moves;
};
class garage
{
private :
char * stack[MAX] ;
int top ;
public :
garage( )
{
top = -1 ;
}
void arrive(struct car c)
{
push(c.lPlate);
}
void depart(struct car c )
{
}
void push ( char * item ) ;
char* pop( ) ;
void show();
} ;
void garage:: push ( char* item )
{
if ( top == MAX - 1 )
cout << endl << "Sorry. Parking lot is full" ;
else
{
top++ ;
strcpy(stack[top] , item) ;
}
cout<<"In push: "<<stack[top];
}
char * garage:: pop( )
{
if ( top == -1 )
{
cout << endl << "Parking lot empty is empty" ;
return NULL ;
}
return stack[top--] ;
}
void garage:: show()
{
cout<<" \nParking Lot Status:\n";
if( top == -1 )
{
cout << endl << "Parking lot is empty" ;
return ;
}
int i=top;
cout<<"In show "<<stack[0]<<endl;
while(i>-1)
{
cout<<stack[i]<<endl; i--;
}
}
int main()
{
clrscr();
char *action;
garage g;
ifstream fin("TEST");
if(!fin){cout<<"Cannot open i/p file \n\n";return 1;}
char str1[80],str2[40]; int i,j;
while(!fin.eof())
{
fin.getline(str2,MAX);
struct car c;//create a car obj
char * pch;
pch = strtok (str2," ,.-");//split string on getting <space>/<,>/<.>/<->
while (pch != NULL)
{
action=pch;
pch = strtok (NULL, " ,.-");
c.lPlate=pch;
pch=NULL;
cout<<"\nAction: "<<action<<" lPlate: "<<c.lPlate<<"\n";
switch(*action)
{
case 'A':
g.arrive(c);
break;
case 'D':
g.depart(c);
break;
}
}
cout<<"\n\n";
}
fin.close();
cout<<"\n";
g.show();
getch();
}