Thread: need help for stack problem again

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    15

    need help for stack problem again

    Can anyone help me,please?
    this is about:

    There is only one entrance/exit to the garage. If someone comes to retrieve a car that is not first out of exit, all cars blocking its path must be moved out (hence the stack). After the car is taken out, all other cars are restored in order they were taking out. Each line of the input file contains an "A" for arrival or "D" for departure, a license plate number (6 characters) and the time of arrival or departure.
    The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify the time of arrival and whether or not there is room for the car in the garage (10 car limits).
    sample output:

    Car with license no. DT3172 arriveg at 8:01 AM car parked

    Car with license no. BFD111 arriving at 12:52 PM no room available

    Car with license no. BFD111 deparing at 1:33 PM no such car parked here

    Car with license no. DT3172 departing at 2:59 PM
    times moved: 2
    time parked: 6 hours and 58 minutes

    Note: when calculating the time parked, first convert the time of arrival and the time of departure to military time.

    Note: use stack

    Code:
    #ifndef GARAGE_H
    #define GARAGE_H
    
    
    const int MAX_CARS= 6;
    
    
    class GarageStack
    {
    public:
    	bool IsEmpty() const;
    
    	bool IsFull() const;
    
    	void Push(char AorD, char tag[], int milhours, int milmin, char AorP[]);
    
    	void Pop(char AorD, char tag[], int milhours, int milmin, char AorP[]);
    
    	GarageStack();
    	
    	struct car
    	{
    		char arrival;
    		char tag[MAX_CARS];
    		int hours;
    		int minutes;
    		char AorP[2];
    	
    	};
    
    
    private:
    	char spaces;
    	int top;
    
    
    };
    #endif
    Code:
    #include <iostream>
    #include "garage.h"
    
    using namespace std;
    
    GarageStack::GarageStack()
    {
    	
    	top= -1;
    }
    bool  GarageStack::IsEmpty()const
    {
    	if(top ==-1)
    	{
    		return true;
    	}
    	return false;
    }
    
    bool  GarageStack::IsFull()const
    {
    if (top== MAX_CARS-1)
    	{
    		return true;
    	}
    	return false;
    }
    
    void  GarageStack::Push(char AorD, char tag[], int milhours, int milmin, char AorP[])
    {
    	if(!IsFull())
    	{
    		top++;
    		spaces[top].arrival= AorD;
    		for (int i=0; i<6; i++)
    			spaces[top].tag[i] = tag[i];
    		
    		spaces[top].hours = milhours;
    		spaces[top].minutes = milmin;
    
    		for (int k=0; k<2; k++)
    			spaces[top].ampm[k]= AorP[k];
    
    		cout<<"Car with license number: ";
    		int i=0;
    		while(i<6 && spaces[top].tag[i]!='\0')
    		{
    			cout<<spaces[top].tag[i];
    			i++;
    		}
    		spaces[top].moved=0;
    		cout<< "arriving at: "<<spaces[top].hours<<":";
    		(spaces[top].minutes<10)?cout<<'0'<<spaces[top].minutes:cout<<spaces[top].minutes;
    		cout<<" Car is Parked "<<endl<<endl;
    	}
    	else
    	{
    		cout<<" Car with license number: ";
    		for (int i=0; i<6; i++)
    			cout<<tag[i];
    		cout<<"\nNo Room Available"<<endl<<endl;
    	}
    }
    
    void  GarageStack::Pop(char AorD, char tag[], int milhours, int milmin, char AorP[])
    {
    	if(!IsEmpty())
    	{
    		bool match=false;
    		int i= top;
    		while(!match && i>0)
    		{
    			match = true;
    			int j=0;
    			while (match && j<6)
    			{
    				if (spaces[i].tag[j] !=tag[j])
    				{
    					spaces[i].moved++;
    					match=flase;
    				}
    				j++;
    			}
    			if (!match)
    				i--;
    		}
    		if (match)
    		{
    			int diffhours;
    			int diffminutes;
    			
    			if(AorP[0]=='P' && Hr !=12)
    				Hr+=12;
    			if (spaces[i].ampm[0]=='P' && soaces[i].hours !=12)
    				spaces[i].hours+=12;
    			diffhours = Hr - spaces[i].hours;
    			diffminutes = Min - spaces[i].minutes;
    
    			if(diffminutes < 0 )
    			{
    				diffhours = diffhours - 1;
    				diffminutes = diffminutes = 60;
    			}
    
    			cout<< "Car with license number: ";
    			int i=0;
    			while ( i<10 && spaces[i].tag[i]!='\0')
    			{
    				cout<<spaces[i].tag[i];
    				i++;
    			}
    			cout<<" departing at: "<<spaces[top].hours<<":";
    			(spaces[top].minutes)<10?cout<<'0'<<spaces[top].minutes:cout<<spaces[top].minutes;
    			cout<<endl;
    			cout<<"Time parked: "<<diffhours<<" Hours and "<<diffminutes<<" minutes "<<endl;
    			cout<<"Time the car was moved: "<<spaces[i].numx<<endl;
    
    			int z;
    
    			cout<<endl;
    			for (z = top; z>=i; z--)
    			{
    				tempspaces[top-z] = spaces[z];
    			}
    			for (z = i; z<=(top-1); z++)
    				spaces[z] = tempspaces[top-1-z];
    			top = top -1;
    		}
    		else
    			cout<<"There is no such car."<<endl;
    	}
    	else 
    		cout<<"The garage is Empty"<<endl;
    }
    Code:
    #include <iostream>
    #include <fstream>
    #include "garage.h"
    #include <stack>
    
    
    using namespace std;
    
    const int MAX = 6;
    
    void main()
    {
    	ifstream infile;
    	GarageStack stack;
    	char AorD;
    	char AorP[2];
    	char tag[MAX_CARS];
    	int hour;
    	int min;
    
    	infile.open  ("garagepark.txt");
    
    	if (infile.fail ())
    	{
    		cout << "Inut file open failed." <<endl;
    		exit(1);
    	}
    
    	infile >> AorD>>tag>>hour>>min>>AorP;
    
    	while (infile)
    	{
    		if (AorD == 'A')
    			stack.Push(AorD,tag,hour,min,AorP);
    	
    		else if (AorD == 'D')
    			stack.Pop(AorD,tag,hour,min,AorP);
    	
    			infile >>AorD>>tag>>hour>>min>>AorP;
    	}
    	infile.close();
    
    }
    need help for space[top], how should I call it from my h file.
    thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote:
    need help for space[top], how should I call it from my h file.
    thanks

    do you mean 'spaces'? if so spaces isnt an array so you cant access it with an index[top]. how do you access these variables? access them from where? in the header you can access them in any other file you cannot.

    if this isnt what you want then make more effort to explain the problem.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> need help for space[top], how should I call it from my h file.
    The [] indicate that spaces is an array. Can you identify what type the array holds? Does it hold chars, ints, strings, cars, or something else? Once you know that type, all you need is a size for the array. My guess is that the size will be some MAX amount, so you have to figure out what to put for the size.

    >> if so spaces isnt an array
    See http://cboard.cprogramming.com/showthread.php?t=85564 for more information. It is my understanding that the source file is "correct" and the header file needs to be updated to match it.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use int main(), not void main(). Oh yes, and exit() is in <cstdlib>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    I get this error:
    subscript requires array or pointer type
    left of '.arrival' must have class/struct/union type
    also for others like tag...

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    spaces isnt an array

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> spaces isnt an array
    It is supposed to be an array. That is bahareh's problem. How to correctly make it an array in the header file.

    See my post above, bahareh.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    you mean like this:
    char spaces[MAX_CARS];

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    that would work yes

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    no, it is not working. the same error:
    left of '.arrival' must have class/struct/union type

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    spaces is now a char array. it doesnt have a member called 'arrival'. the struct 'car' does, i havent looked at the logic of the program so i dont know what you need, as i didnt need to look at it to solve your initial question. maybe it should be a car instead.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Please need more explanation?!?!?!

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you identify what type the array holds? Does it hold chars, ints, strings, cars, or something else?

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    it holds chars and ints

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    something is wrong here. i think you want maybe an array of 'car's. car is the struct that holds 'arrival' which your trying to access with the spaces array. i think you want maybe an array of cars with a size of your already-defined MAX_CARS const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM