garage simulation

This is a discussion on garage simulation within the C++ Programming forums, part of the General Programming Boards category; I have a task where I have to simulate a parking garage in C++ using stack. There is only one ...

garage simulation

I have a task where I have to simulate a parking garage in C++ using stack. 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.

I am provided with an input file which should be read in my program. 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.

ie. ACDA98310:30AM
DCDA983 2:15PM
........

My first question about this program is:

I am not very familiar with reading in files of this type, so the program will have to know how to filter each part of the input. How do I do that?

Also, will I have to use 2 stacks? 1 for the main garage, and another as a temporary stack to store the cars when I am popping them out of garage to allow for a departure?

Read-in one line at a time. then, parse the line... Separate the A/D, the license number, and the time. (I assume that the times in file are in order.)

You only need ONE STACK. Arrival means push onto the stack, and depart means pop off of the stack. The stack holds the current state. Push & pop are actions that change the state... you don't need to "store" these actions. (Except in the file that is the source of the data.)

I have 2 structs that I need to use one for the reading of input data called: "struct car" and another called: "struct time", which will be needed to calculate the time parked in garage and I will first need to convert time to military time to make it easier.

do these structs need their own .h file?

here are my two structs thus far:

Code:

struct time{
int hours;
char : ;
int minutes;
};
struct car{
char A_or_D; //used for Arrival or Departure in input file *not sure if this is right?
char license[6]; //license number of 6 characters in input
time carTime; //this is the instance of the above struct time
char AM_PM[2];
int num_moves; //progam needs to record the # times car was moved
};

Hey Doug, just read your message, thanks, you break it down nicely, I am very new and really trying to grasp this. Now beginning with the reading the input file. You mention read one line at a time and then parse the line, how do I do this?

I don't see why you need a structure. I think you need a stack that simply holds license numbers. You might want to have a variable that holds the "current" time... the time of the most-recent event. Then, at any given time-of-day, your program could list the cars (license numbers) that are in the stack... and of course, which car is on the top of the stack (coming out next).

Your instructor will probably give you a data file, and ask: At the end of this sequence, how many cars are in the garage? Which car would come out next? What are the license numbers of the cars in the garage? Note that with these questions, your program doesn't need to know about the time.

You can read-in each line of text as a string. You can use a C-style character array, or a C++ style string, if you've studied them already. The first character (character-zero) of the string is either an 'A", or a 'D'. Characters 1 thru 6 are the license number. I'm sure that you already know how to copy these partial strings into new character-variables & strings.

If you can't figure out how to convert the time-portion of the string into a time-structured variable... well, you probably can ignore it... It depends on if your program has to report the status of the garage at a particular time, or just at the end of the sequence.

Sorry, I should have mentioned the specifics of the program and the output needed. I will state the directions I was given:

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 limit). If there is no room, the car leaves without entering the garage. When a car departs, the message should include the time of departure, the number of times the car was moved out of garage to allow other car(s) to depart, and the time the car was parked in the garage in hours and minutes. If a person tried to claim a car which was never parked at the garage, an appropriate error message should be printed.

Note: The only way to tell if a car is not parked at the garage is to move all the cars out of the garage.

If each line has the two tokens separated by a space then use the >> operator to read the items into one string each directly.

If there is no space between the licence number and the time stamp and the licence is always six char long then you can use getline() to limit intake of just the first six char or use a loop and read in just one char per loop six times, placing null char after sixth char read in. If you used getline() then call it again with appropriate parameters to read in the time stamp, or use a loop to read in the time stamp, whichever. Discard the time stamp if you don't need it.

If the project develops to the point where you need to develop a protocol for charging for the parking based on how long each car is in the garage, then you will want to save the time. You could use the two classes as you did, or just put the three variables representing the time into the car class as members.

I think you will need two stacks. One to represent the cars in the garage and the other to keep track of those you move out and then move back. Say cars ABCDE are in garage in that order. C wants to leave. You have to move D and E out to get C out. If you put them on a second stack and then remove them from the second stack and put them back in the first, they will remain in order.

to separate the hour and min and PM/AM you can read the data char by char. Put the first char in the hour string. If the second char is a colon, put a null char at the end of the hour string, ignoring the colon. Else put the second char in the hour string and ignore the next char. then put the next one or two char in the min string testing by using isdigit() or isalpha() or is 'A' || 'P', or wahtever you know how to do.
Then read AMPM string in. Then convert the hour string and the min string to ints using atoi() or stringstream or whatever. Then if AMPM string equals PM add twelve to the hour string. Then substract starting time from current time, accounting for rollover from one day to the next, if necessary.

I am still really unclear about how to use the getline() feature is there a good tutorial on how to use it effectively?

To your question about spacing between the license number and the time stamp, in some cases there is a space and in other cases there is no space, let me show you an example:

AOSBORN 9:12AM
DTCA147 9:50AM
AARK74V11:13AM
ACDF94312:43PM

As you can see, the first is either A or D for Arrive or Depart and the next 6 (always 6 characters) is the license number, and then in some cases if if 10,11,12 then there will be no space between the license and time, however, if its a single digit before the colon, then there will be a space

I'd probably do a char by char read in from file. Char 1 to arrive/depart. Char 2 through 7 for license. Check char 8 for space and ignore if it is, else place it in the hour string. Then I'd read in the rest of the time stuff char by char as described previously.

Ok, that clears how to read the input, but how i translate that to code, this is something I have alwasy been weak on, and always seems to be a hitch in my development. Could you please explain how to translate your method to code. You dont have to use my example, and in essense give me my answer, I am here to learn. It would really help if you show me a template to use, and the logic behind it from the computers standpoint. My question exactly would be, how do tell the program to read in the first letter, assign it to a variable already declared, and so on...

1) declare ifstream and associate it with file to read
2) be sure ifstream is open and ready for use
3) declare variables arrive/depart, license, hour, min, AMPM, hourString, minString, stackOne, stackTwo
4) declare dummy char to use as read in.
5)start loop to read in
6) loop conditional is ifstream name >> dummy char.
7) assign dummy char to arrive/depart
8) use loop to read in next six char for license
9) add null char to license
10) use loop to read in char into dummy until colon found
11) if char is space, then ignore
12) else add to hourString
13) when dummy is colon, add null to hour
14) hour loop now done so use another loop to read char into minString.
15) when char is 'A' || 'P' add null char to min string
16) hour = atoi(hourString);
17) if dummy, from 15 == 'P' add 12 to hour.
18) min = atoi(minString);
19) account for carry over to next day if need be
20) add dummy from 15 to AMPM string
21) use loop to read in the MPM\n one char at a time
22) if arrive/depart is 'A', push car onto stackOne
23) else pop stackOne and add to stackTwo each car in stackOne until car with same license is found.
24) calculate hours in garage
25) display results
26) put cars in stack two back into stack one
27) go back to 5)

This char by char parsing of file could be done on string representing line read in from file, as previously suggested by someone else.

ifstream fin("myFile.txt");
string fileLine;
while(getline(fin, fileLine))
in body of while parse fileLine char by char using equivalent of 7 to 26 above looking at each element/char in fileLine one at a time and placing it in appropriate variable based on position or value.