Code isn't executing correctly

I am writing a program that sells tickets for a theatre. I need to have the user enter the price for the ticket they want, then my program will give them a seat. I am trying to make it display a message when there are no more seats for the price they enter. I can't find out how to do this. My current code displays the message every time they specify a price. I only want it to display when there are no more seats for that price.
This is the part of my code that I'm having trouble with.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
int ticket_by_price;
bool found = false;
int pos = 0;
cout << "Please choose how much you want to spend on your ticket.\nTickets cost $10, $20, $30, $40, and $50.\n";
cout << "------------------------------------------\n$";
cin >> ticket_by_price;				
while (pos < 90 && !found)
{
	if (seating_chart[pos][pos] == ticket_by_price)
	{
		found = true;
	}
	else
	{
		pos++;					}
	}
	if (!found)
	{
		cout << "Sorry, there are no more seats available for that price.\n";
	}


This is the program

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
using namespace std;

const int ROWS = 9;
const int COLUMNS = 10;

int display_menu();
int display_seating_chart();
int seat_by_price(int);
int seating_chart[ROWS][COLUMNS] =
{
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
};


int main()
{
	int quit = 0;	

	cout << "\n~~~~~~~~~~~~ Welcome to the Theatre! ~~~~~~~~~~~~~\n\n";
	cout << "Tickets prices range from $10, $20, $30, $40, and $50";
	cout << "\nNo tickets have been sold yet. All seats are available.\n\n";
	display_seating_chart();

	do
	{
		display_menu();
		int choice;
		cin >> choice;

		int temp_row;
		int temp_seat;

		switch (choice)
		{
		case 1: cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
			cout << "These are the seats that are currently available. Seats with '0' have already been sold.\n";
			cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
			display_seating_chart(); system("pause"); break;
		case 2: int choose; cout << "------------------\n1) Specific seat\n2) Any seat by $\n------------------\n";
			cout << "Please choose how you would like to purchase your ticket: "; 
			cin >> choose;
			cout << "------------------------------------------------------------\n";			
				
			if (choose == 2)
			{
				int ticket_by_price;
				bool found = false;
				int pos = 0;
				cout << "Please choose how much you want to spend on your ticket.\nTickets cost $10, $20, $30, $40, and $50.\n";
				cout << "------------------------------------------\n$";
				cin >> ticket_by_price;				
				while (pos < 90 && !found)
				{
					if (seating_chart[pos][pos] == ticket_by_price)
					{
						found = true;
					}
					else
					{
						pos++;
					}
				}
				if (!found)
				{
					cout << "Sorry, there are no more seats available for that price.\n";
				}					
				ticket_by_price = seat_by_price(ticket_by_price);				
			}
			else
			{
				cout << "Please choose a valid option:\n";
			}
			system("pause"); break;
		case 3: cout << "Thank you for using this program!\n"; quit = -1; break;
		}
	} while (quit != -1);

	system("pause");
	
	return 0;
}


int display_menu()
{
	cout << "\nMovie Theatre Menu\n";
	cout << "----------------------\n";
	cout << "1) Show seating chart\n";
	cout << "2) Purchase tickets\n";
	cout << "3) Quit the program\n";
	cout << "----------------------\n";
	cout << "Please enter what you would like to do: ";
	return 0;
}

int display_seating_chart()
{
	//Seats header
	for (int seats_header = 0; seats_header < 11; seats_header++)
	{
		if (seats_header == 0)
		{
			cout << "Seats| ";
		}
		else if (seats_header < 2)
		{
			cout << setw(3) << seats_header;
		}
		else if (seats_header < 11)
		{
			cout << setw(4) << seats_header;
		}
	}
	cout << endl;
	for (int seats_header = 0; seats_header < 11; seats_header++)
	{
		if (seats_header < 11)
		{
			cout << "====";
		}
	}
	cout << "====" << endl;
	//Rows
	for (int row = 0; row < ROWS; row++)
	{
		switch (row)
		{
		case 0: cout << "Row 1|  "; break;
		case 1: cout << "Row 2|  "; break;
		case 2: cout << "Row 3|  "; break;
		case 3: cout << "Row 4|  "; break;
		case 4: cout << "Row 5|  "; break;
		case 5: cout << "Row 6|  "; break;
		case 6: cout << "Row 7|  "; break;
		case 7: cout << "Row 8|  "; break;
		case 8: cout << "Row 9|  "; break;
		}
		// Seating Chart Array
		for (int column = 0; column < COLUMNS; column++)
		{
			cout << setw(2) << seating_chart[row][column] << "  ";
		}
		cout << endl;
	}
	return 0;

}
int seat_by_price(int ticket_by_price)
{	
	switch (ticket_by_price)
	{
	case 10:
		for (int row = 0; row < 9; row++)
		{
			for (int column = 0; column < 10; column++)
			{
				if (seating_chart[row][column] == 10)
				{
					seating_chart[row][column] = 1;
					cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $10\n";
					cout << "--------------------------------------\nPurchase was successful\n";
					return seating_chart[row][column] = 0;
				}
			}
		}
		break;
	case 20:
		for (int row = 0; row < 9; row++)
		{ 
			for (int column = 0; column < 10; column++)
			{
				if (seating_chart[row][column] == 20)
				{
					seating_chart[row][column] = 2;
					cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $20\n";
					cout << "--------------------------------------\nPurchase was successful\n";
					return seating_chart[row][column] = 0;
				}
			}
		}
		break;
	case 30: 
		for (int row = 0; row < 9; row++)
		{
			for (int column = 0; column < 10; column++)
			{
				if (seating_chart[row][column] == 30)
				{
					seating_chart[row][column] = 3;
					cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $30\n";
					cout << "--------------------------------------\nPurchase was successful\n";
					return seating_chart[row][column] = 0;
				}
			}
		}
		break;
	case 40:
		for (int row = 0; row < 9; row++)
	{
		for (int column = 0; column < 10; column++)
		{
			if (seating_chart[row][column] == 40)
			{
				seating_chart[row][column] = 4;
				cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $40\n";
				cout << "--------------------------------------\nPurchase was successful\n";
				return seating_chart[row][column] = 0;
			}
		}
	}
		break;
	case 50: 
		for (int row = 0; row < 9; row++)
		{
			for (int column = 0; column < 10; column++)
			{
				if (seating_chart[row][column] == 50)
				{
					seating_chart[row][column] = 5;
					cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $50\n";
					cout << "--------------------------------------\nPurchase was successful\n";
					return seating_chart[row][column] = 0;
				}
			}
		}
		break;
	}
	return 0;
}
Don't return a meaningless 0 from your functions. If you have nothing meaningful to return, make the function void.

You have a lot of repetition in seat_by_price. Don't just copy and paste and change a couple of values. The fact that you need to change so little is a clue that it can be done better. E.g.,
1
2
3
4
5
6
7
8
9
10
11
12
13
void seat_by_price(int ticket_by_price)
{	
	for (int row = 0; row < 9; row++)
		for (int column = 0; column < 10; column++)
			if (seating_chart[row][column] == ticket_by_price)
			{
				seating_chart[row][column] = ticket_by_price / 10;
				cout << "Seat " << column + 1 << ", in Row " << row + 1 << ", is available for $" << ticket_by_price << "\n";
				cout << "--------------------------------------\nPurchase was successful\n";
				seating_chart[row][column] = 0;
				return;
			}
}


As for your problem, this is strange:
 
	if (seating_chart[pos][pos] == ticket_by_price)

You are only checking down the diagonal!
Since you are having pos go from 0 to 89, you could index the array like this:
 
	if (seating_chart[pos/10][pos%10] == ticket_by_price)

Thank you very much for helping me out! And also thank you for the tips as well, really appreciate it! Will try my best to make my future codes less repetitive. I am having trouble understanding why my problem was only checking down the diagonal? I just started learning about arrays so I don't really know about them all that well
I am trying to account for when the user enters a value that isn't part of the prices. This is what I have :

1
2
3
4
5
6
if (ticket_by_price != 10 || ticket_by_price != 20 || ticket_by_price != 30 || ticket_by_price != 40 || ticket_by_price != 50)
{					
	cout << "Please enter a valid option.\nTickets cost $10, $20, $30, $40, and $50.\n";
	cout << "------------------------------------------\n$";
	cin >> ticket_by_price;					
}


But it keeps executing even if the user inputs a valid option. I don't see what's wrong with this
Last edited on
Hello Jorge626,

Try changing '||' to '&&'. The logical and tends to work better with "!=".

Andy
if (ticket_by_price != 10 || ticket_by_price != 20 || ticket_by_price != 30 || ticket_by_price != 40 || ticket_by_price != 50)

Hi,

I personally dislike hate constructs like that even when done correctly. They are not scalable: what if you had 20 or 50 options here? And they are just plain UGLY !

It's better to put in a default: case in a switch , much tidier. Or put all the valid values into a std::vector , use one of the find functions, if the value is not found print the error, and loop again.

With the do loop, I prefer a while loop controlled with a bool value when the user want's to quit, make a case for it in the switch.
Alright got it! Thanks for all your help and suggestions!
Topic archived. No new replies allowed.