Program: Selling seats

Hello everyone.

I'm supposed to write a code that'll sell seats in a three level auditorium for three different days (Days: Thursday, Friday, and Saturday (T, F, & S); Sections: Floor, Balcony, and Upper Balcony (F, B, & U)) The problem is, I can't even get the sell_seat function to work.

I was wondering if anyone could help me figure out what was wrong with my code (I keep getting
98 [main] william_rodriguez_prog5 6884 open_stackdumpfile: Dumping stack trace to william_rodriguez_prog5.exe.stackdump
whenever I try to run that option.

Anyone know why?

Also, I was hoping someone could point me in the right direction for the other functions in the program. I'm having trouble printing out the seating chart.

print_chart( ): displays seating chart;
print_day_list( ): displays the sales summary, ordered by the days;
print_section_list( ): displays the sales summary, ordered by the section.

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
# include <iostream>
# include <iomanip>
# include <cctype>

using namespace std;

char get_menu_choice(char choice);

void sell_seat(char seats[][3][20]);
void print_chart(char seat_chart[3][20]);
/*
 void cashier(int var_2);
 void print_day_list(int var_4);            //Functions I need help with.
 void print_section_list(int var_5);
 */

char choice;
char seats[3][3][20];
char seat_chart[3][20];

int main()
{
	cout << "Initializing seating chart." << endl << " " << endl;

	int d, s, n;
	for (d = 0; d < 3; d++) {
		for (s = 0; s < 3; s++) {
			for (n = 0; n < 20; n++)
				seats[d][s][n] = '-';
		}
	}

	cout << "All seats have been initialized." << endl << " " << endl;

	do {
		choice = get_menu_choice(choice);
		choice = toupper(choice);

		switch (choice) {
		case 'S':
			sell_seat(seats);
			break;

		case 'C':
			cout << "You chose C!" << endl;
			break;

		case 'D':
			cout << "You chose D!" << endl;
			break;

		case 'F':
			cout << "You chose F!" << endl;
			break;

		case 'Q':
			cout << "Quit" << endl;
			break;

		default:
			cout << "Invalid selection. Try again." << endl;
			break;
		}

		cout << " " << endl;
	} while (choice != 'Q');

	return 0;
}

char get_menu_choice(char choice)
{
	cout.fill(' ');
	cout << setw(22) << "*** Main Menu ***" << endl << "S - Sell a Ticket."
			<< endl << "C - Display Seating Chart." << endl
			<< "D - Display Sales Summary - Day Listing" << endl
			<< "F - Display Sale Summary - Floor Listing" << endl << "Q - Quit"
			<< endl << "Your Choice: ";

	cin >> choice;

	return choice;
}

void sell_seat(char seats[][3][20])
{
	int d, s, n;
	char seat;
	seats[d][s][n] = '-';

	cout << "Enter seat request by day (T)hursday, (F)riday, or (S)aturday"
			<< endl
			<< "followed by section (F)loor, (B)alcony, or (U)pper Balcony"
			<< endl << "followed by seat number (1 - 20)." << endl;
	cout << "Seat: ";
	cin >> seat;

	d = toupper(d);
	switch (d) {
	case 'T':
		d = 0;
		break;
	case 'F':
		d = 1;
		break;
	case 'S':
		d = 2;
		break;
	default:
		cout << "Invalid choice try again";
		break;
	}

	s = toupper(s);
	switch (s) {
	case 'F':
		s = 0;
		break;
	case 'B':
		s = 1;
		break;
	case 'U':
		s = 2;
		break;
	default:
		cout << "Invalid choice try again";
		break;
	}

	if (n < 0 || n > 20)
		cout << "Invalid seat number." << endl;
	else
		n -= 1;

	if (seats[d][s][n] == '*')
		cout << "This seat has been taken." << endl;
	else {
		seats[d][s][n] = '*';
		cout << "Congratulations! You got the seat." << endl;
	}
}

void print_chart(char seat_chart[3][20])
{
   //I don't know how to start this.
   //I know it has to be a 2D array, though.
}


Any help is greatly appreciated, I need this in a few days and I'm starting to freak out a bit.
I suspect seats[d][s][n] = '-'; at line 89 is the first culprit

The entire function sell_seat(char seats[][3][20]) has uninitialized variables.
int d, s, n; and then d = toupper(d); similar case for s = toupper(s);

These uninitialized variables are going to cause problems. Assign proper values to these plus I see seat variable is not used at all in the function
Last edited on
Line 87: d, s and n all uninitialized (garbage).
Line 89: You try to reference a seat using d, s and n. Guaranteed to crash your program.

Line 18-19: What's the difference between seats and seat_chart?

Line 143: Since you say it needs to be a 2D array, I'm assuming you are printing the seating chart for a single day. You simply need to call print_chart with the appropriate day's seating chart.
1
2
 
  print_chart (seats[1]);  // Example for Friday 

Then in print_chart iterate through the sections and seats.

Lines 9-10: I would strongly suggest using constants for the maximum number of days, sections and seats. This allows you to easily change the seating configuration.
1
2
3
const int max_days = 3;
const int max_sections = 3; 
const int max_seats = 20;

Use these constants in both your array declarations and your loop conditions.

Lines 17-19: Avoid the use of globals. Move these inside main.
Last edited on
If I assign values to d, s, & n, wouldn't just that element in the array be initialized as ' - '?

As for the 'seat' variable, I want to have it to where the user inputs something like TF18 and have the program pass that into the array as an index for the element. This case, TF18 would be indexed as seats[0][0][17]. I think I have it wrong in the program though.
What is it that you think line 89 should do?

As written, it's going to try and store '-' into one element of the array, where the subscripts are garbage. As I said before, guaranteed to crash your program.
AbstractionAnon: I apologize, I had read your response after I had typed out mine and refreshed. I will try out your suggestions about using the constants and moving the variables. Thanks!

As for the seats and seat_chart, I was going to use the seats as the main seating array and the seat_chart as the array to print out the seating chart, but now I'm starting to think that might not be a good idea.
Last edited on
I'm starting to think that might not be a good idea

You're right, it's not a good idea. You want one array to represent the status of the seats, rather than trying to maintain the status in two places. The trick here is to pass part of the array (one day) to print_chart.

You were right about the constants for number of days, rows, and seats. That allowed my function to run.

Now, as to passing just a single part of the array to the print_chart function, how would I do that?

I was thinking something along the lines of:

1
2
3
cout << "Thursday" << endl;
cout.fill(' ');
cout << setw(9) << "Floor:" << setw(8);


but I'm not sure how I would get it to print out the '-' and '*' to look like this:
http://imgur.com/F5aLToL

I'm thinking a loop of some sort?
Topic archived. No new replies allowed.