theatre seat reservation

my code is not working can any1 check it and tell me whats wrong or can sme1 plz write the program for me using 2d arrays

Write your question here.
Write a program that does reservations for a theatre with 5 rows of seats, and 9 seats per row. The rows are numbered from ā€˜Aā€™ to ā€˜Eā€™ (back to front) and the seats from 1 to 9 in each row. The user should be presented with a seat plan (showing the current bookings) from which a number of seats in one row can be booked. For example


1 2 3 4 5 6 7 8 9
A - - - - - - - - -
B - - - - - - - - -
C - - - - - - - - -
D - - - - - - - - -
E - - - - - - - - -
In which row do you want seats? A
How many seats? 2
starting at which number? 4

1 2 3 4 5 6 7 8 9
A - - R R - - - - -
B - - - - - - - - -
C - - - - - - - - -
D - - - - - - - - -
E - - - - - - - - -

In which row do you want seats? A
How many seats? 5
Starting at which number? 3
PROBLEM: A3 is already reserved
In which row do you want seats? Q

Store the data in a two-dimensional array of characters. Initially all the elements of the array contain the character -, to indicate that the seats are available. When a reservation , the program must determine if the requested seats are available and if they are, it must reserve them. A reservation is made by assigning the character R to the elements in question. If any of the requested seats is already reserved (or the requested seats fall outside the seat plan), the entire reservation must be rejected with an appropriate message. The user must enter Q (for the row) to quit the program.

#include <iostream>

using namespace std;
char seats[6][10]={
{' ','1','2','3','4','5','6','7','8','9'},
{'A','-','-','-','-','-','-','-','-','-'},
{'B','-','-','-','-','-','-','-','-','-'},
{'C','-','-','-','-','-','-','-','-','-'},
{'D','-','-','-','-','-','-','-','-','-'},
{'E','-','-','-','-','-','-','-','-','-'}
};

void reservation(int row,int col,int numOfSeat)
{
for (int r=0;r<6;r++)
{
for(int col=0;col<10;col++)
{
cout << seats[r][col]<<" ";
}
cout << endl << endl;
}


for(int i=numOfSeat;i!=0;i--){
if(seats[row][col]=='-')
{
seats[row][col]='R';
col--;
}
else if(seats[row][col]=='R'){
char Q;
if(row==1){
cout << "PROBLEM: " << 'Q' << col << " is already reserved" << endl;
}
col--;
}
}


for (int r=0;r<6;r++)
{
for(int col=0;col<10;col++)
{
cout << seats[r][col]<<" ";
}
cout << endl << endl;
}
}
int main ()
{
do{
char ro;
int row,num,start;
cout<<"In which row you want seat? ";
cin>>ro;
cout<<"How many seats? ";
cin>>num;
cout<<"Starting at which number? ";
cin>>start;
if(ro=='A') {row=1
}
else if(ro=='B'){row=2
}
else if(ro=='C'){row=3
}
else if(ro=='D'){row=4
}
else if(ro=='E'){row=5
}

reservation(row,start,num);
}while (true);
system("PAUSE");
return 0;
}
Last edited on
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
// theater seat reservation // anup 28.10.2014
#include <iostream>
using namespace std;

bool reservation(int row, int start, int num, bool seats[5][9]) {

for (int i=start; i<=start+num-1; i++)
	if(seats[row][i] == 1)
		return false;	

for (int j=start; j<=start+num-1; j++)
	seats[row][j] = 1;	
	
return true;
}


int main () {
bool seats[5][9] = {0}; // all empty at start
	bool b, tryAgain=1;
	char ro;
	int row, num, start;
	
	do
	{
	cout << "Current Reservation:\n";
	for (int i=0; i<5; i++)
		for (int j=0; j<9; j++)
			{
				cout << seats[i][j] << " ";
				if (j==8) cout <<"\n";
			}			

	cout<<"In which row you want seat? ";
	cin>>row;
	cout<<"How many seats? ";
	cin>>num;
	cout<<"Starting at which number? ";
	cin>>start;	
	
	if(row<1 || row>5 || num <1 || num >9 ||start < 1 || start >9 || start+num>9 )
	{
		cout << "invalid input. try again\n\n";
		continue;
	}
	
	b = reservation(--row, --start, num, seats);

	if(b) cout << "seats allocated!\n";
	else cout << "could not be allocated!\n";
	
	cout << "try again ?(0 to stop) ";
	cin >> tryAgain;
	cout << "\n\n";
	} while (tryAgain);

//cin.ignore();
return 0;
}
yudz:

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
// Store the data in a two-dimensional array of characters.
// Initially all the elements of the array contain the character -,
// to indicate that the seats are available. 
// You already did this 
int main()
{
char seats[7][10]={
{' ','1','2','3','4','5','6','7','8','9'},
{'A','-','-','-','-','-','-','-','-','-'},
{'B','-','-','-','-','-','-','-','-','-'},
{'C','-','-','-','-','-','-','-','-','-'},
{'D','-','-','-','-','-','-','-','-','-'},
{'E','-','-','-','-','-','-','-','-','-'},
{'F','-','-','-','-','-','-','-','-','-'}
};
// declare your variables:
char row;
int seat =0 , number = 0;

// set up a while loop to accept user entries, either like 
cin >> row, seat, number;  // or individually like 
cin >> row;
cin>> seat;
cin>> number;

// If you accept a char as the row you'll have to convert it to int for use as an index in your array:

switch (row){
case 'a': // if row is type char, use single quotes, if type string, use double
case' A':
   rowint = 1;
   break;
case 'b':
case 'B':
   rowint = 2;
   break;
// etcetera through row 'F'
// you could also check for entry of 'Q' and act accordingly
// any out of range entries should restart the loop

//When a reservation , the program must determine if the requested seats are available.  
// will the set of seats needed extend past the end of the row?
bool goodchoice = (seat + number <= 9 ) 
//user's choice is tentatively okay, but we need to check each seat individually:

If any of the requested seats is already reserved (or the requested seats fall outside the seat plan), reject the reservation

for (int i = seat; i < seat + number -1 && goodchoice; i++)
{ goodchoice = (seats[rowint][i] != 'R'); }

//
If they are available, it must reserve them. 
if (goodchoice)
{ // replace the same seats as the previous for statement with 'R';}

//A reservation is made by assigning the character R to the elements in question. 
else
{ The entire reservation must be rejected with an appropriate message.}
// The user must enter Q (for the row) to quit the program.
//since we're in a loop the above shouldn't be too difficult. . .
}
Last edited on
Topic archived. No new replies allowed.