Using pointers an string arrays

im supposed to build a simple traffic control program.. i'm done but i kno its full of errors it runs but not the results i want.. can someone please help me implement some pointers or string manipulation into the code.. i'm having problems with that.. the code is completely commented, so you may get an idea of the results desired

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// A program to control the flow of traffic between two streets

// Juice

// Traffic Control v1 29-10-2013

#include <iostream>
#include <cstring> // to do string manipulations

using namespace std;

void streetsA();
void streetsB();
void lanesA();
void lanesB();

	// using array to represent streets and lanes
	string streetA [1] [2] = {"StreetALane1", "StreetALane2"};
	string streetB [1] [2] = {"StreetBLane1", "StreetBLane2"};
	string laneA [1] [2]= {"LaneAL1", "LaneALane2"};
	string laneB [1] [2] = {"LaneBLane1", "LaneBLane2"};


int main()
{


	// variable declaration
	int approach;

	// the for loop to represent two drivers to test program
	for (int car = 2; car < 3; car++)
	{

        // do loop to get the driver experience
		do
		{
		// Streets and Lanes
		cout << "1. STREET A\n";
		cout << "2. STREET B\n";
		cout << "3. LANE A\n";
		cout << "4. LANE B\n";


		do
		{
		// driver chooses the street route before intersection
		cout << " Choose Street Or Lane To Approach Intersection: ";
					cin >> approach;
					cout << "\n\n\n";
				} // end do

				while (approach <1 || approach >4);


                  // switch statement to choose which from street A or B and Lane A or B driver will be approaching intersection
		switch (approach)
		{
			 case 1:
//flash yellow light to indicate that driver should slow down as he approches intersection
                        cout <<  "\t\t\t\tYellow Light\n\n\n\n\n";
 			streetsA();
			break;

		case 2:
                       cout <<  "\t\t\t\tYellow Light\n\n\n\n\n";
			streetsB();
			break;

		case 3:
                        cout <<  "\t\t\t\tYellow Light\n\n\n\n\n";
			lanesA();
			break;

                    case 4:
                        cout <<  "\t\t\t\tYellow Light\n\n\n\n\n";
                        lanesB();
                        break;

				} // end switch





		} // end do

				while(approach == car);

	} // end for loop

		return 0;
} // end main

        // operations of streetA
		void streetsA()
		{
			// variable declaration
			int lane;

			// streets and lanes driver can take from position
			cout << "1." << streetA[1][1] << "\n";
			cout << "2." << streetB[1][1] << "\n";
			cout << "3." << laneA[1][1] << "\n";
			cout << "4." << laneB[1][1] << "\n";

                        // driver is at intersection
			cout <<  "\n\t\t\t\tRed Light\n\n\n\n\n";  

			cout << "Choose route Desired: ";
			cin >> lane;
			cout << "\n\n";



            // switch statement for driver route options
	switch (lane)
		{
		case 1:
				    
                        //make lane clear from streetA lane1 to streetB lane2
			streetA[0][1] = streetB[1][1];

                   // if lane is clear flash green light, if not flash red light
			if(streetA[0][1] == streetB[1][1])
				{ cout << "\t\t\t\tGreen Light\n"; }

                            else cout << "Red Light\n";

                 // stop all other traffic that can possibly go to streetB lane2
                            if(laneB[0][1] == streetB[1][1])
				{ cout << "\t\t\t\tRed Light\n"; }

					break;
		case 2:

			streetA[0][1] = laneA[1][1];

                        if (streetA[0][1] == laneA[1][1])
                        { cout << "\t\t\t\tGreen Light\n"; }

                            else cout << "Red Light\n";

                 if (streetB[0][1] == laneA[1][1] || laneB[0][1] == laneA[1][1])
                          { cout << "\t\t\t\tRed Light\n"; }

                                    break;
		case 3:
                    streetA[0][1] = laneB[1][1];

                        if (streetA[0][1]== laneB[1][1] )
                            { cout << "\t\t\t\tGreen Light\n"; }

                            else cout << "Red Light\n";

                            if(streetB[0][1] == laneB[1][1])
                                { cout << "\t\t\t\tRed Light\n"; }

                                break;
			} // end switch

		} // end member function streetsA

        // operations of streetB
		void streetsB()
		{
            // variable declaration
			int lane;
                // driver has to stop before taking a route

                  cout << "\t\t\t\tRed Light\n\n"; 

			// streets and lanes driver can take from position
			cout << "1." << streetA[1][1] << "\n";
			cout << "2." << laneA[1][1] << "\n";
			cout << "3." << laneB[1][1] << "\n\n";

			cout << "Choose route Desired\n\n";
			cin >> lane;

            // switch statement to show driver route options
			switch (lane)
			{
				case 1:
			// make lane clear from streetB lane1 to streetA lane2
					streetB[0][1] = streetA[1][1];

                   // if lane is clear flash green light, if not flash red light
					if(streetB[0][1] == streetA[1][1])
                        { cout << "\t\t\t\tGreen Light\n"; }

                            else cout << "\t\t\t\tRed Light\n";

                  //stop all other traffic that can possibly go to streetA lane2
                            if(laneB[0][1] == streetA[1][1])
                                { cout << "Red Light\n"; }

                                        break;
		          case 2:
				streetB[0][1] = laneA[1][1];

				if (streetB[0][1] == laneA[1][1])
                                    { cout << "\t\t\t\tGreenLight\n"; }

                            else cout << "\t\t\t\tRed Light\n";

                            if(streetA[0][1] == laneA[1][1])
                                { cout << "Red Light\n"; }

                                    break;
			case 3:
			        streetB[0][1] = laneB[1][1];

			if (streetB[0][1] == laneB[1][1])
                            { cout << "\t\t\t\tGreen Light\n"; }

                            else cout << "\t\t\t\tRed Light\n";

                            if(streetA[0][1] == laneB[1][1])
                                { cout << "Red Light\n"; }

                                  break;
			} // end switch
		} // end member funtion streetsB

        // operations of laneA
void lanesA()
	{

             // driver has to stop before taking a route
               cout << "\t\t\t\tRed Light\n\n"; 

            // make lane from laneA lane1 to streetB lane2 clear
            laneA[0][1] = streetB[1][1];

            // if lane is clear flash Greenlight if not flash red light
		if (laneA[0][1] == streetB[1][1])
                    { cout << "\t\t\t\tGreen Light\n"; }

                    else cout << "\t\t\t\tRed Light\n";

                    // stop all other traffic leading to streetB lane2
            if (streetA[0][1] == streetB[1][1] || laneB[0][1] == streetB[1][1])
                        { cout << "Red Light\n"; }

		} // end memberfuntion lanesA


        // operations of laneB
		void lanesB()
		{
		    // variable declaration
		     int lane;

	   cout <<  "\t\t\t\tRed Light\n\n\n\n\n";  // driver is at intersection

		    cout << "1." << streetsA[1][1] << "\n";
		    cout << "2." << streetB[1][1] << "\n";

            // driver chooses route desired
		    cout << "Choose Route Desired: ";
		    cin  >> lane;

            // switch statement to show driver route options
		    switch(lane)
		    {
		        case 1:
		            // make lane clear from laneB lane1 to streeA lane2
		            laneB[0][1] = streetA[1][1];

                   // if lane is clear flash Green Light, if not flash red light
                    if (laneB[0][1] == streetA[1][1])
                        { cout << "Green Light\n"; }

                        else cout << "Red Light\n";

                    // stop all other traffic that could possibly take that lane
                        if(streetB[0][1] == streetA[1][1])
                            { cout << "Red Light\n"; }

                            break;

                case 2:
                    laneB[0][1] = streetB[0][1];

                    if (laneB[0][1] == streetB[1][1])
                        { cout << "Green Light\n"; }

                        else cout << "Red Light\n";

                        if(streetA[0][1] == streetB[1][1])
                            { cout << "Red Light\n"; }

                            break;

		    } // end switch

		} // end member function lanesB

Have you covered Data Structures and the keywords struct or classs?

There is a problem with the syntax of those string things, but the real problem is that you're using the wrong data structure to represent your roads.

As I understand it, you have the concept Road, which as Lanes; an ordered set of lanes to be more technical.

You should then implement that data structure in your code and use it.

Having said that, I looked at your errors. The real problem is you're using all those 2D arrays, when in fact, you should be using 1D arrays.

For example, you have:
 
string streetA [1] [2] = {"StreetALane1", "StreetALane2"};


But shouldn't it be this?
 
string streetA[2] = {"StreetALane1", "StreetALane2"};
i've done a course on data structures last semester. got a b-.. i took your advice and and got it back down to a one dimensional and it wors pretty fine now.. thanks alot kbw..
i'd just love to use a few pointers in there to.. i try to make the array a string pointer but its an error.. seems i have a bit more research to do but thanks alot..
Topic archived. No new replies allowed.