Problems programming lists

Still slowly learning c++ and im trying to write code that displays a mock parking lot. When the user arrives he/she is asked whether they are picking up or dropping off a vehicle. If they are dropping off then the car is supposed to be pushed into a list. If they are picking up it then displays all the vehicles in each lane. It then asks which lane to choose from, when you choose a lane it then reads out each car asking whether or not its their vehicle. When their vehicle is found its removed. The logic seems right to me but there seems to be major flaws when i try to build it.

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
#include <iostream>
#include <string>
#include <list>
#include "ListP.h"
#include "ListP.cpp"

using namespace std;

int main ()
{
	string a,b,c,e,h;
	int f=1;
	int g;
	int i = 0;
	list<string> lane1(10), lane2(10);

	while (a!="x" || a!="X")
	{
		cout << "Welcome to the parking lot! \n \n";
		cout << "If your picking up a car press P \n";
		cout << "If your dropping off a car press D \n";
		cout << "To view all the cars in the lot press V \n";
		cout << "Or to exit press X \n \n";
		cin >> a;

		if(!(a=="P"||a=="p"||a=="d"||a=="D"||a=="V"||a=="v"||a=="X"||a=="x"))
		{
			cout<< "Im sorry but thats an incorrect input \n";
			return 0;
		}
	


	if(a=="x"||a=="X")
	{
		break;
	}

	else if (a=="D"||a=="d")
	{

		cout<< "Please enter the vehicle license plate";
		cin>> b;

		if((lane1.size) < 10)
		{
			lane1.insert(1,b);
			cout<< "You are parked in lane 1";

		}

		else if((lane2.size) < 10)
		{
			lane2.insert(1,b);
			cout<< "You are parked in lane 2";
		}
		else if (lane1.size && lane2.size > 10)
		{
			cout<<"The lots are full, please come back later";
		}
	}

	else if (a=="p"||a=="P")
	{

		cout << "These are the cars in the lot";

		while ((lane1.size) > 1)
		{
			cout<< "The license plates in lane 1 are ";

			while (f < 10)
			{
				lane1.retrieve(f, b);
				cout<< b << ", ";
				f++;
			}
		}

		f=1;
		cout<< endl;

		while ((lane2.size) > 1)
		{
			cout<< "and the license plates in lane 2 are ";

			while (f < 10)
			{
				lane2.retrieve(f, b);
				cout<< b << ", ";
				f++;
			}
		}

		f=1;

		cout<< "If you are parked in lane 1 press 1, if your parked in lane 2 press 2" << endl;

		cin >> g;

		while (g==1)
		{
			lane1.retrieve(f,b);
			cout<< "Is your plate number " << b << "?";
			cin >> h;

			if (h=="y")
			{
				lane1.remove(f);
				break;
				break;
			}

			while (h=="n")
			{
				f++;
				lane1.retrieve(f,b);
				cout<< "How about " << b << "?" << endl;
				cin >> h;
			}

			break;

		}


		while (g==2)
		{
			lane2.retrieve(f,b);
			cout<< "Is your plate number " << b << "?";
			cin >> h;

			if (h=="y")
			{
				lane2.remove(f);
				break;
				break;
			}

			while (h=="n")
			{
				f++;
				lane2.retrieve(f,b);
				cout<< "How about " << b << "?" << endl;
				cin >> h;
			}

			break;

		}
		break;
		}

		
		}





		return 0;
	}
Last edited on
1
2
3
4
5
if(!(a=="P"||a=="p"||a=="d"||a=="D"||a=="V"||a=="v"||a=="X"||a=="x"))
		{
			cout<< "Im sorry but thats an incorrect input \n";
			return 0;
		}

This part is unnecessary... At the end of your else-if ladder, just put:

1
2
3
4
5
else
{
        cout<< "Im sorry but thats an incorrect input \n";
	return 0;
}


Also, at line 57:
 
else if (lane1.size && lane2.size > 10)

is incorrect syntax... It should be:

 
else if (lane1.size>10 && lane2.size > 10)


In all your while and if conditions, the letter should be in single quotes... like line 34, 39, 63, etc:
 
if (a=='D'||a=='d')


Also, at line 110, why are there two break; statements?

At line 101, it is an infinite loop, but because you use break;, its ohk. Why not use 'if' instead of while? you won't have the risk of an infinite loop, and neither will you have to use break...
Last edited on
Line 101-124: What happens if the user first enters n, and then y? you take input in h in the second loop, and then do nothing. It should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (g==1)
{
                 do
		 {
                    
			lane1.retrieve(f,b);
			cout<< "Is your plate number " << b << "?";
			cin >> h;
                       
			if (h=="y")
			{
				lane1.remove(f);
				break;
			}
                        
                         f++;
                 }
		 while (h=="n");
}


Do the same for g==2...
Thanks for the help!
:)
Topic archived. No new replies allowed.