Help with c++ program.

Hey guys i need with this program everytime i run it nothing is outputing i don't know how to fix it.
Thanks in advance. By the way i am new to this forum so i don't if this is the right place to post this thanks.

Lab Assignment #2
The Parking Garage
The CSC326 Parking Garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrance/exit to the garage at one end of the lanes.

If a customer arrives to pick up a car which is not nearest the exit, all cars blocking the cars' path are moved into the other lane. If more cars still must be moved out of the way, they go into the street. When the customer's car is driven out, all cars in the street must be put back into the garage.
Write a C++ program that reads input from a file (that you create). Each line in the file contains two fields separated by a blank: a code (A = an arriving car, or D= a car wishes to depart) and a license plate number (this could be a string). Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs.
When a car arrives, the message should specify whether or not there is room in the garage for the car. If there is no room, the car leaves without entering. When a car departs, the message should include the number of times the car had to be moved out of the way so that other cars could depart. Each move from one lane to the other counts as 1; each move to the street counts as 1; each move from the street to a lane counts as 1. Don't forget to check for conditions such as someone wanting a car that's not in the garage, trying to park a car but both lanes are full, trying to park a car when only one lane is full, etc.
Your program should define an object from a 'garage' class to represent the 2 lanes and the street. The garage class will contain three stack objects one for each lane and the street. Use the dynamic array implementation of the stack. You'll need methods for manipulating the cars in the lanes, e.g. search for a car in a lane, move a car from a lane to somewhere, and perhaps higher-level methods like arrive and depart and methods to handle your output. This is NOT a complete list of methods needed, so feel free to experiment and expand the class definitions as much as you like. You may find it easier to have a car class or structure that contains the license plate and a counter for the number of moves the car makes.

Here are the program it has 3 files a cpp and 2 headers.

The text file is name ParkingGarage.txt
it suppose to read this inputs
A 123ABC
A 345XYZ
D 123DEF
A 674GTX
A 896YUX
D 234FDS
A 567TYD
A 891JKL
D 435GHD
A 786IOC

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
Main
#include<iostream>
#include<string>
#include<fstream>
#include <cstdlib>
#include"Garage.h"
#include"Stack.h"
using namespace std;

int main()
{
	GarageD car;

	ifstream infile;
	ofstream outfile;

	infile.open("ParkingGarage.txt");
	if (infile.fail())
	{
		cout << "file can not open!" << endl;

	}
	string newcar, newcar2;
	while (!infile.eof())
	{
		infile >> newcar;
		infile >> newcar2;

		if (newcar == "a")
		{
			car.arrive(newcar2);
		}
		else
			if (newcar == "d")
				car.depart(newcar2);
}
	infile.close();
	return 0;
}





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
Garage.h

#include<iostream>
#include<string>
#include"stack.h"
#ifndef Garage_h
#define Garage_h
using namespace std;

struct car
{
	int moves;
	string plateNumber;
public:
	bool operator==(const car&x)
	{ return(plateNumber == x.plateNumber); }
};

class GarageD
{

private:
	car a;
	car c;
	Stack<car> lane1;
	Stack<car> lane2;
	Stack<car> street;

public:
	GarageD();
	void arrive(string vehicle);
	void depart(string vehicle);

};
GarageD::GarageD()
{

}

void GarageD::arrive(string vehicle)
{
	if (!lane1.IsFull())
	{
		cout << vehicle << " has been parked in line 1.\n";
		a.plateNumber = vehicle;
		a.moves = 0;
		lane1.push(a);

	}
	else if (!lane2.IsFull())
	{
		cout << "line1 is full, the car is moving to line2" << endl;
		cout << vehicle << " has been parked in line 2.\n";
		a.plateNumber = vehicle;
		a.moves = 0;
		lane2.push(a);
	}
	else
	cout << "both lines are full, your car was not parkerd!" << endl;

	
}
void GarageD::depart(string vehicle)
{
	a.plateNumber = vehicle;
	c.plateNumber = vehicle;

	cout << endl;
	if (lane1.Search(a))
	{
		while (a.plateNumber != lane1.Top().plateNumber)
		{
			c = lane1.Top();
			c.moves++;
			if (!lane2.IsFull()) {
				lane2.push(c);
				lane1.pop();
			}
			else {
				street.push(c);
				lane1.pop();
			}
		}
		
		c = lane1.Top();
		c.moves++;
		cout << "Car is in line1, and it has been depart from line 1."<<c.plateNumber<<"Total number of moves"<<c.moves << endl;
		lane1.pop();
	}
	else if (lane2.Search(a))
	{
		while (a.plateNumber != lane2.Top().plateNumber)
		{
			c = lane2.Top();
			c.moves++;
			if (!lane1.IsFull())
			{
				lane1.push(c);
				lane2.pop();
			}
			else {
				street.push(c);
				lane2.pop();
			}
		}
		c = lane2.Top();
		c.moves++;
		cout << "The car depart from line 2."<<c.plateNumber <<"Total number of moves"<<c.moves<< endl;
		lane2.pop();
		
	}

	while (!street.IsEmpty())
	{
		if (!lane1.IsFull())
		{
			c = street.Top();
			c.moves++;
			lane1.push(c);
			street.pop();
		}
		else
		{
			c = street.Top();
			c.moves++;
			lane2.push(c);
			street.pop();
		}
	}
}

#endif 




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
Stack.h
// file Stack.h
// array stack implementation
#ifndef Stackh
#define Stackh
#include <cstdlib>

template<class StackType>
class Stack {
	// LIFO objects

public:
	Stack(int MaxStackSize = 5);
	~Stack() { delete[] stack; }
	bool IsEmpty() const { return top == -1; }
	bool IsFull() const { return top == MaxTop; }
	StackType Top() const;
	void push(StackType x);
	void pop();
	bool Search(StackType x);

private:
	int top;    // current top of stack
	int MaxTop; // max value for top
	StackType *stack;   // element array
};

template<class StackType>
Stack<StackType>::Stack(int MaxStackSize)
{
	//Pre: none'
	//Post: Array of size MaxStackSize to implement stack
	// Stack constructor.
	MaxTop = MaxStackSize - 1;
	stack = new StackType[MaxStackSize];
	top = -1;
}

template<class StackType>
StackType Stack<StackType>::Top() const
{
	//Pre: stack is not empty
	// Post:  Returns top element.
	if (IsEmpty())
		throw logic_error("Top fails: Stack is empty");// Top fails
	return stack[top];
}

template<class StackType>
void Stack<StackType>::push(StackType x)
{
	//Pre: Stack is not full
	//Post: Push x to stack.
	//		Stack has one more element
	if (IsFull()) throw logic_error("Push fails: full stack"); // Push fails
	stack[++top] = x;
}

template<class StackType>
void Stack<StackType>::pop()
{
	//Pre: Stack is not Empty
	//Post: Stack has one less element
	if (IsEmpty()) {
		throw logic_error("Pop fails: Stack is empty");
		exit(1);
	}; // Pop fails
	top--;
}

template<class StackType>
bool Stack<StackType>::Search(StackType x)
{
	for (int i = 0; i <= top; i++)
	{
		if (*(stack + i) == x)
			return true;
	}

	return false;
}

#endif
Last edited on
i don't if this is the right place to post this thanks.


This is the right place. Welcome.

First, please go back and edit your post. Highlight the text contained in each of your 3 files and then click the "<>" format button for each one. This will format the code better to make it easier to read and comment on your code.

Next, your Garage.h file should probably be broken up into 2 files--a .h file and a .cpp file.

But to your actual question. How much works?

Maybe start with a single lane and a single car. Can you park the car and see that it is parked? You should use a debugger or print out a debug message identifying the contents of your stack to make sure it contains the correct contents.

Add a second car. Make sure your stack contains what it is supposed to.

Now park a single car and then remove it.

Then park 2 cars in the same lane and remove the first.

Then add the second lane. etc. etc.

If you start small and add on after you've got it working, you'll more likely find your bugs as they are introduced.

If you write code for the whole project before testing it (the "big bang" approach), finding your bugs will prove to be much harder.
Only 3 files requires for this program according to the professor. For "how much works" i am not sure because the code look fine i previously had errors when i run it and once i fixed it no error in the code. But when i run it its just output nothing all i get is "Press any key to continue".

while (!infile.eof())
Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


You're checking for lower case actions, but the text file contains upper case actions. The result is you read the file, but take no actions.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
When comes to file i suck at doing them honestly that's how i learned it so far. This is how i was taught to write it. I am still new to c++, so if i were to rewrite do i have to remove the whole while (!infile.eof()) where would i put the (!infile.eof()).
This is how i was taught to write it.

Yes, that's quite a common way to teach file access. Unfortunately, it's wrong D:

You should perform the read as the loop condition, instead of (!infile.eof()). You don't need to test for eof() unless you are querying why a read failed. It's potentially a good idea to check for eof() at the end of the loop
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  } if (! stream.eof()) { /* you've not read the entire file; has something gone wrong? */ }
so that you can verify that the read failed because there's no more records, instead of failing for some other reason (in which case eofbit wouldn't be set, but failbit or badbit would be).
Last edited on
what should i include in the while (stream >> var) as i never seen this before until now so i am not sure where to add that along with what should include in the stream and var unless thats how it is and doesnt require to change it and leave as is.
Basically the idea is this.
Replace this existing code
1
2
3
4
5
6
    string newcar, newcar2;
    while (!infile.eof())
    {
        infile >> newcar;
        infile >> newcar2;
        // etc. 

with this modified version:
1
2
3
4
    string newcar, newcar2;
    while (infile >> newcar >> newcar2)
    {
        // etc. 


The rest of the code (where I put etc.) remains the same.
The second version does what the first was supposed to do - but more reliably.

Still doesn't output anything i added the modified version and still nothing is displaying in the output console. This is so annoying i have full set of code no errors and nothing is displaying in the output console all i get is "press any key to continue".
Thank you for editing your post and adding code tags.

First snippet, lines 29 and 34. You haven't fixed the lower case problem I pointed out earlier.
Your text file contains "A" and "D" for actions, while your code is looking for "a" and "d". You will never find a match on action and never call arrive() or depart().

Try this:
 
  if (newcar == "a" || newcar == "A")
Wow man thank you guys for the help a simple fix was the "A" and "D" and the text files you are right AbstractionAnon it was looking for lower case while i have uppercase in the text files. Thanks man, you know the funny part about all this i show it to my prof and she didn't even point that out she even run it herself and told the same things i told you guys about it not outputing anything. Like wow all this and she didn't even take the time to be oh i see it you just had to fix your uppercases. Its working now thanks again guys.
Topic archived. No new replies allowed.