seat reservation program

Hello, I am trying to complete this code. The object is to write a program that can reserve seats (like a movie theater) using a two-dimensional array. The user enters the number of rows and number of seats. Then they can choose which seat they would like to fill (ie. 2F, where 2 is the row and F is the seat.) I have most of it written, but I am having trouble with the task of verifying that a valid row and seat were entered as well as keeping a count of the seats as they are filled and displaying a message once all seats are filled. The large comment block in the middle of my code is where i am having trouble.

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
  #include <iostream>
#include <cctype>
using namespace std;


char ** CreateArrayOfSeats (int NumberRows, int seats);
void InitializeSeats (char **ArrayOfSeats, int NumberRows, int seats);
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int seats);
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int seats);


int main(int argc, char * argv[]) {
    char **ArrayOfSeats;
    char rowSelect;
    char seatSelect;
    char answer;
    int NumberRows;
    int NumberSeats;
    int row;
    int seat;
    
    cout << "Welcome to your personalized seating reservation system!" << endl;
    cout << "Please enter the number of rows: ";
    cin >> NumberRows;
    cout << "Please enter the number of seats in each row: ";
    cin >> NumberSeats;
    
    
    ArrayOfSeats = CreateArrayOfSeats(NumberRows, NumberSeats);
    InitializeSeats(ArrayOfSeats, NumberRows, NumberSeats);
    DisplaySeats(ArrayOfSeats, NumberRows, NumberSeats);
    
    
    do {
        cout << endl;
        cout << "Please enter a seat selection (Example 5F): " << endl;
        cin >> rowSelect;
        cin >> seatSelect;
        
        if (rowSelect== '0')
            continue;
        
        seatSelect = toupper(seatSelect);
        row = rowSelect - '1';
        seat = seatSelect - 'A';
        
        /////////////// write the rest of the code here /////////////////////
        /////////////// verify if a valid row and seat were entered /////////
        /////////////// see if all the seats are taken, keep a count ////////
        /////////////// (use a counter) each time a seat is taken ///////////
        /////////////// use loop to ask if they would like to choose ////////
        /////////////// another seat (y/n) //////////////////////////////////

        if (ArrayOfSeats[row][seat] == '-')
            cout << "The seat you selected has already been taken, please try another seat." <<endl;
        else
            ArrayOfSeats[row][seat] = '-';
        
    } while (rowSelect != '0');
    
    
    MemoryCleanup(ArrayOfSeats, NumberRows, NumberSeats);
    
    cout << "Press the ENTER key to continue.";
    char buff[100];
    cin.getline (buff, 100);
    
    return 0;
}

//// CreatArrayOfSeats Function
char **CreateArrayOfSeats (int NumberRows, int seats) {
    char **ArrayOfSeats;
    ArrayOfSeats = new char*[NumberRows];
    for (int r=0; r<NumberRows; r++)
        ArrayOfSeats[r] = new char[seats];
    return ArrayOfSeats;
}

///// InitializeSeats Function
void INitializeSeats (char **ArrayOfSeats, int NumberRows, int seats) {
    for (int r=0; r<NumberRows; r++) {
        for (int s=0; s<seats; s++)
            ArrayOfSeats[r][s] = 'A' + s;
    }
}

///// DisplaySeats Function
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
    for (int r=0; r<NumberRows; r++) {
        cout.width(2);
        cout << r+1 << ' ';
        for (int s=0; s<NumberSeats; s++)
            cout << ArrayOfSeats[r][s] << ' ';
        cout << endl;
    }
}

///// MemoryCleanup Function
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
    for (int r=0; r<NumberRows; r++)
        delete [] ArrayOfSeats[r];
    delete [] ArrayOfSeats;
}
Last edited on
@bblotz
It's great seeing that you enclosed your code in the code block. Thanks.

I tweeked your code a bit, and added in the row/seat checks.
If you have any questions about the code presented, just ask, but checking it over should answer a lot of them. ;)

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

using namespace std;

char ** CreateArrayOfSeats (int NumberRows, int seats);
void InitializeSeats (char **ArrayOfSeats, int NumberRows, int seats);
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int seats);
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int seats);


int main(int argc, char * argv[]) {
	char **ArrayOfSeats;
	char Select[3];
	char answer;
	int NumberRows;
	int NumberSeats;
	int Seats_Available;
	int row;
	int seat;

	cout << "Welcome to your personalized seating reservation system!" << endl;
	cout << "Please enter the number of rows: ";
	cin >> NumberRows;
	cout << "Please enter the number of seats in each row: ";
	cin >> NumberSeats;
	Seats_Available = NumberRows*NumberSeats;

	ArrayOfSeats = CreateArrayOfSeats(NumberRows, NumberSeats);
	InitializeSeats(ArrayOfSeats, NumberRows, NumberSeats);
	DisplaySeats(ArrayOfSeats, NumberRows, NumberSeats);

	do {
		do
		{
			cout << endl;
			cout << "There are " << Seats_Available << " seats available, to choose from."<<endl;
			cout << "Please enter a seat selection (Example 5F): " << endl;

			cin >> Select;
			row = Select[0] - '1';
			if(Select[1] >= '0' && Select[1] <= '9') // In case there are more than 9 rows
			{
				row += 1;
				row *= 10;
				row += (Select[1] - '1');
				Select[1] = Select[2];
			}
			Select[1] = toupper(Select[1]);
			seat = Select[1] - 'A';
			if(row < 0 || row > NumberRows-1)
				cout << "Row selections go from 1 to " << NumberRows <<". Try a different row.." << endl;
			if(seat < 0 || seat > NumberSeats-1)
				cout << "Seat selections go from A to " << char('A'+ (NumberSeats-1)) << ". Try a different seat.." << endl;
			

		}while ( row < 0 || row > NumberRows-1 || seat < 0 || seat > NumberSeats-1);
		if(row >= 0)
		{
			if (ArrayOfSeats[row][seat] == '-')
				cout << "The seat you selected has already been taken. Please try another seat." <<endl;
			else
			{
				ArrayOfSeats[row][seat] = '-';
				Seats_Available--;
				
			}
			if(Seats_Available == 0)
				cout << "There are no more seats available, so the program is ending." << endl; 
		}
		DisplaySeats(ArrayOfSeats, NumberRows, NumberSeats);		
		do
		{
			cout << endl << "Reserve another seat? (Y/N)" << endl;
			cin >> answer;
			answer = toupper(answer);
			if(answer != 'Y' && answer != 'N')
				cout << "I check only for a 'Y' or an 'N' response!" << endl;
		}while(answer != 'Y' && answer != 'N');
		if(answer == 'N')
			row = -1;
	} while (Seats_Available > 0 && row >= 0 );
	
	MemoryCleanup(ArrayOfSeats, NumberRows, NumberSeats);

	cout << endl << "Program has ended." << endl;
	cout << endl << "Press the ENTER key to continue." << endl << endl;
	char buff[100];
	cin.getline (buff, 100);

	return 0;
}

//// CreatArrayOfSeats Function
char **CreateArrayOfSeats (int NumberRows, int seats) {
	char **ArrayOfSeats;
	ArrayOfSeats = new char*[NumberRows];
	for (int r=0; r<NumberRows; r++)
		ArrayOfSeats[r] = new char[seats];
	return ArrayOfSeats;
}

///// InitializeSeats Function
void InitializeSeats (char **ArrayOfSeats, int NumberRows, int seats) {
	for (int r=0; r<NumberRows; r++) {
		for (int s=0; s<seats; s++)
			ArrayOfSeats[r][s] = 'A' + s;
	}
}

///// DisplaySeats Function
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
	for (int r=0; r<NumberRows; r++) {
		cout.width(2);
		cout << r+1 << ' ';
		for (int s=0; s<NumberSeats; s++)
			cout << ArrayOfSeats[r][s] << ' ';
		cout << endl;
	}
}

///// MemoryCleanup Function
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
	for (int r=0; r<NumberRows; r++)
		delete [] ArrayOfSeats[r];
	delete [] ArrayOfSeats;
}
Last edited on
Thank you very much, I understand now what needed to be done. :)
Topic archived. No new replies allowed.