Need help with this arrays moving into functions

So im having some serious problems here. I'm trying to so far debug the program before moving on and im having trouble passing an array to a function (in purchase tickets).

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
  // Theater Array Project
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int rows = 15;
const int cols = 30;


//Function Prototypes
void displayMenu();
int getChoice();
void displaySeats(string[]);
void displayPrices(double[]);
void displaySales();
bool purchaseTicket();


int main ()
{
	//DECLARATION SECTION
	double rowPrice [15];
	int choices;
	string seats[15][30];
	
	//GETTING PRICE FOR TICKECTS BASED ON THE ROW
for (int price = 0; price<15; price++)
	{
		cout<<"Please enter ticket price for Row "<<setw(2)<<price+1<<": ";
		cin>>rowPrice[price];
	}
	
//FUNCTION CALLS
	displayMenu();
	choices=getChoice();
		//processing choices
		while(choices != 5)
{
	//Display available seats
	if (choices == 1)
	{
		displaySeats(seats);
		
		
	}
	//Display row prices
	else if (choices == 2)
	{
		displayPrices(rowPrice);
		
		
	}
	//Display ticketSales
//	else if (choices == 3)
//	{
//		displaySeats();
//		
//		
//	}
	//Display Ticket purchase option
	else if (choices == 4)
	{
		purchaseTicket();
		
	}
	displayMenu();
	choices = getChoice();
}

	return 0;
}




//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//++++++++++++++++THIS FUNCTION DISPLAYS THE MENU+++++++++++++++
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void displayMenu ()
{
  cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
  cout << "\n\t1. View Available Seats";
  cout << "\n\t2. View Seating Prices";
  cout << "\n\t3. View Ticket Sales";
  cout << "\n\t4. Purchase a Ticket";
  cout << "\n\t5. Exit the Program\n\n";
  

}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//~~~~~~~~~~~~~~~~GET CHICES FROM THE USER(1-5)~~~~~~~~~~~~~~~~~~~~~
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int getChoice()
{
	int choice;
	cout << "\n\tEnter your choice(1-5): ";
	cin>>choice;
	while (choice<1 || choice >5)
	{
		
		cout<<"Choice must be between 1 and 5. Please re-enter: ";
     	cin>>choice;
		
	}
	
	return choice;
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//~~~~~~~~~~~~~~~~~~~~~~THIS FUNCTION DISPLAY THE SEATS~~~~~~~~~~~~~~~
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	void displaySeats(seats)
	{
	
cout << "\n\t\tSeats";
  cout << "\n        123456789012345678901234567890" << endl;
  
 
  
 
  
  
  for(int price=0; price<15; price++)
  {cout<<"Row "<<setw(2)<<price+1<<"  ";
  for(int prices=0; prices<30; prices++)
  {
  seats[price][prices]='#';
  cout<<seats[price][prices];
  }
  cout<<endl;
  }
  cout << "\n\n\n\tLegend:\t* = Sold";
  cout << "\n\t\t# = Available";
  cout << "\n\n\nPress the Enter key to continue.";
  cin.ignore();
  cin.get();
	}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//()()()()()()()()()()()()THIS FUNTION DISPLAY THE PRICE OF OF TICKETS DEPENDS
//~~~~~~~~~~~~~~~~~~~~~~~~ON THE ROW(1-15)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void displayPrices(double prices[15])
{
	
cout << "\nTicket Prices By Row " << endl << endl;
cout << "      Row    Price" << endl;
cout << "      ---    -----" << endl;

for (int row=0;row<15;row++)

{

cout<<setw(8)<<row+1<<setw(10)<<fixed<<setprecision(2)<<prices[row]<<endl;

}
cout << "\n\n\nPress the Enter key to continue.";
  cin.ignore();
  cin.get();
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//===================THIS FUNCTION LET THE USER PURSHASE A TICKET===================
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool purchaseTicket()
{
int row;
int column;
char answer;	

cout << "\n\t\t C++ Theatre" << endl;
cout << "\t\t Ticket Purchase Opportunity" << endl << endl;
cout << "Do you wish to view the chart of available seats \n"<< "before making your selections (y/n)? ";

cin>>answer;
if(answer=='y'||answer=='Y')	
{
displaySeats(seats[15][30]);

	
	cout<<"Please enter desired row (1-15): ";
	cin>>row;
	while (row<1||row>15)
	{
	cout<<"\nRow must be between (1-15) ";
	cin>>row;
	}
	cout<<"\nPlease enter desired seat number (1-30): ";
	cin>>column;
	while (column <1 || column > 30)
	{
		cout<<"Seat must be between 1 and 30. Please re-enter: ";
		cin>>column;
	}
	cout<<"\nPurchase confirmed\n";
}

else 
{
	cout<<"\nPlease enter desired row (1-15): ";
	cin>>row;
	while (row<1||row>15)
	{
	cout<<"Row must be between (1-15) ";
	cin>>row;
	}
		cout<<"\nPlease enter desired seat number (1-30): ";
	cin>>column;
	while (column <1 || column > 30)
	{
		cout<<"Seat must be between 1 and 30. Please re-enter: ";
		cin>>column;
	}
	cout<<"\nPurchase confirmed\n";
}
}
What kind of problems are you having? Compile errors? Run time errors? Not getting the correct result? Please be specific.

Line 15: Your function prototype is incorrect. You've said that diplaySeats() takes a 1D array. It should take a 2D array.
 
void displaySeats(string[][cols]);


Line 44: This line is correct, but the function prototype for displaySeats() says it takes a 1D array.

Lines 24,26,29,126,128,145,152,186,193,205,212: If you're going to define constants for the number of rows and columns, use the constants. Don't hard code values.

Line 115: Your function signature is incorrect. It must agree with the function prototype.

Line 181: You're trying to pass the seats array, but the seats array is undefined. It's a local variable in main. You need to pass the array to purchaseTickets as an argument.

You're calling displaySeats() with an out of bound reference to a single seat. You need to pass the entire seats array to displaySeats(). You don't need to specify the size of the array here.
Last edited on
sorry wrong one
Last edited on
I see you ignored my advice about using defined constants for array sizes and went so far as to remove them (lines 8 and 9 in your original post).

Line 115: seats is a local variable. It goes out of scope when you exit displaySeats? How do you expect to retain the status of the seats across calls to displaySeats()?

string is overkill for saving the status of a seat. char is perfectly adequate.

Line 180: purchaseTicket has no visibility to the seats array. You need to declare the seats array in main() and pass it to both displaySeats() and purchaseTicket().

lines 118,120: price and prices are odd (and confusing) names for indexes. r and c are more commonly used for a 2D seating array.

line 122: You're unconditionally setting each seat to '#'. That does not belong in a function to display the status of seats. Each time you call displaySeats(), you're going to set every seat back to unoccupied. You should have a separate function to initialize the seating array that is called once at the beginning of your program.

Line 216: You need to check if the seat is available here. Loop back and ask the user for a different selection if the seat has been sold. If available, then you need to mark it as unavailable here.
ah im sorry i just realized that was the wrong one -_-


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
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//                                      -THE THEATRE PROJECT-                      
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//   -INCLUDES FILES-  
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//        -FUNCTION DECLARATIONS-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void displayMenu();
int getChoice();
void displaySeats(char[][30]);
void displayPrices(double[]);
void total(double[]);
void purchaseTicket(double[], char[15][30]);




//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//           -MAIN FUNCTION-
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
int main()
{
	//_____________DECLARATION SECTION______________
	
	double rowPrice [15]; //array to store ticket price for each row
	char seats[15][30];
	int choices; //variable o hold the user choice (1-5)
	
	//__________________GET DATA______________________
	
//COLLECTS PRICE FOR EACH ROW (1-15)-
for (int price; price<15; price++)
	{
	 cout<<"Please enter ticket price for Row "<<setw(2)<<price+1<<": ";
	 cin>>rowPrice[price];
	}
	
	
//_____________________ MENU___________________
	
	do
	{
	//DISPLAY MENU (1-5)
	displayMenu();
	
	//GET THE USER CHOICE (1-5)
			// execute the getChoice function and asign its return value to choices
    cout << "\n\tEnter your choice(1-5): ";
	cin>>choices;
			//input validation
	while (choices<1 || choices >5)
	{
	cout<<"Choice must be between 1 and 5. Please re-enter: ";
   	cin>>choices;
	}	
	
	//CALL FUNCTION BASED ON CHOICE(1-4)
	if (choices==1)//if true call the available seats function
	displaySeats(seats);
	
	else if  (choices==2)//if true display prices for each row
	displayPrices(rowPrice);
	
	else if (choices==3)
	cout<<"nothing for now\n";

	else if (choices==4)
	purchaseTicket(rowPrice, seats);

   } 
  while(choices !=5);//when 5 is entered the program terminates
cin.ignore();
  cin.get();
	
	
	
	
	
return 0;	
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//       -DISPLAYS MENU (1-5)-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void displayMenu ()
{
  cout << "\n\n\n\t\tC++ Theatre" << endl << endl;
  cout << "\n\t1. View Available Seats";
  cout << "\n\t2. View Seating Prices";
  cout << "\n\t3. View Ticket Sales";
  cout << "\n\t4. Purchase a Ticket";
  cout << "\n\t5. Exit the Program\n\n";
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//       -GET A CHOICE (1-5)-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//        -DISPLAYS AVAILABLE SEATS-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void displaySeats(char seats[][30])
{
	
cout << "\n\t\tSeats";
cout << "\n        123456789012345678901234567890" << endl;
  
  // to hold an address for each seat in the theatre
 
  
 //loop to display seats and row number
  for(int price=0; price<15; price++)
  {cout<<"Row "<<setw(2)<<price+1<<"  ";
  for(int prices=0; prices<30; prices++)
  {
  seats[price][prices]='#';
  cout<<seats[price][prices];
  }
  cout<<endl;
  }
  
  //display the legend of the chart
  cout << "\n\n\n\tLegend:\t* = Sold";
  cout << "\n\t\t# = Available";
  cout << "\n\n\nPress the Enter key to continue.";
  cin.ignore();
  cin.get();
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//       -DISPLAYS PRIECES FOR EACH ROW-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void displayPrices(double prices[15])
{
//displays the header of the chart	
cout << "\nTicket Prices By Row " << endl << endl;
cout << "      Row    Price" << endl;
cout << "      ---    -----" << endl;

//loop that goes thru the array
for (int row=0;row<15;row++)
{
cout<<setw(8)<<row+1<<setw(10)<<fixed<<setprecision(2)<<prices[row]<<endl;
}

//go back to the menu
cout << "\n\n\nPress the Enter key to continue.";
  cin.ignore();
  cin.get();

}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//   -DISPLAYS TOTAL AMOUNT OF $ OF TICKETS SOLD-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@	
void total(double rowprice[])
{
	
	cout<<"Total sales to date:  $";
	
	
	
	
	
	
	
	
	
	
	
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//              -PURCHASE A TICKET-
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void purchaseTicket(double rowPrice[15],char seats[15][30])
{
int row;
int seat;
double totalprice;

cout << "\n\t\t C++ Theatre" << endl;
cout << "\t\t Ticket Purchase Opportunity" << endl << endl;
cout << "Do you wish to view the chart of available seats \n"<< "before making your selections (y/n)? ";

char answer;
cin>>answer;
//validate whether the user wants to see available seats 
if(answer=='y'||answer=='Y')	
	 {
     displaySeats(seats);
	 }
	 
//get the user desired seat
cout<<"Please enter desired row (1-15):";
cin>>row;
	
		while (row<1||row>15)
	{
	cout<<"Row must be between 1 and 15. Please re-enter: ";
	cin>>row;
    }
    
cout<<"Please enter desired seat number (1-30): ";
cin>>seat;
		while (seat<1||seat>30)
	{
	cout<<"Seat must be between 1 and 30. Please re-enter:";
	cin>>seat;
    }
    if(seats[row-1][seat-1] == '#') // If seat is available
    {
		seats[row-1][seat-1] == '*'; // Mark seat as taken
		
	
	}
//	else
//	{
//		cout<<"Sorry. That seat has been sold."<<endl;
//	}
 cout<<"\nPurchase comfirmed\n";
 
 //get the total price of the user's ticket number 1
 row=row-1;
 totalprice=rowPrice[row];

 
 
//let the user purchase another seat
char respond;
int rownumber;
int seatnumber;
int numbertickets=1;
double ticketprice;


cout<<"\nwould you like to purchase another seat (y/n)? ";
cin>>respond;
if (respond=='y'||respond=='Y')
numbertickets++;

while(respond=='y'||respond=='Y')
  {
	cout<<"Please enter desired row (1-15): ";
	cin>>row;
		while (row<1||row>15)
	{
	cout<<"Row must be between 1and 15.Please re-enter: ";
	cin>>row;
    }
	cout<<"Please enter desired seat number (1-30): ";
	cin>>seat;
		while (seat<1||seat>30)
	{
	cout<<"Seat must be between 1 and 30. Please re-enter: ";
	cin>>seat;
    }
    
    
cout<<"\nPurchase comfirmed";
cout<<"\n\nwould you like to purchase another seat (y/n)? ";
cin>>respond;

//get the user's price of all tickets bough

row=row-1;
totalprice=+rowPrice[row];
//number of tickets counter
if (respond=='y'||respond=='Y')
numbertickets++;

}


cout<<"\n\nYou have purchase a total of "<<numbertickets<<" tickets for a total price of $"<<totalprice<<endl;

}

 
 
 
	
	
Topic archived. No new replies allowed.