Assigning seats for a flight using 2d arrays and functions

You are the developer for taxiflite.com, a company which charters a light aircraft to
Capetown from Johannesburg. Write a program to assign seats on each flight of the
airline's only plane (capacity: 50 seats (10 rows (A-J) of 5 seats each)). The program
should display the following menu of alternatives: Please type 1 for "First class" and
Please type 2 for "Economy". If the person types 1 your program should ask for the
person’s name and assign a seat in the first class section (rows 1-3 (15 seats)). If the
person types 2, the program should ask for the person’s name and assign a seat in
economy section (rows 4-10 (35 seats)). Menu Option 3 displays the seating
allocation. The program should print a boarding pass indicating the person’s name
and seat number and weather it is in the first class or economy section of the plane
along with the price (R10 000 for first class and R5 000 for economy). Isle seats (A-J
seat number 2 and 3 are subject to a 10% discount) Use a 2-Dimensional array to
represent the seating chart of the plane. Initialize all the elements of the array to 0 to
indicate that all seats are empty. As each seat is assigned, set the corresponding
elements of the array to X to indicate that the seat is no longer available. When the
first class section is full, the program should ask the person if it is acceptable to be
placed in the economy section (and vice versa). If yes then make the appropriate
seat assignment. All seats in each section are assigned randomly unless the person
flying stipulates a specific seat. In the display view, show the percentage occupied of
the total flight (seats taken vs seats available) else if no seats are available then print
the message "Next Flight leaves in 3 hours." All flight information must be saved to a
file.
Use Function for initialize, menu, checkSeat, printTicket, displaySeating

Hi, i urgently need help.
My code compiles, but i have the following problems
1. For my number 1 option in my switch statement, when i enter "1", nothing happens.
2. when i ask clients their seat numbers they are going to enter something like "A1; B3", how do i do that
3. how would you code something like, if economy is full and you ask if they want to go to First class and assign a seat for them there
4. does my code make sense??
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

001	//
002	#include <iostream>
003	#include <string>
004	#include <fstream>
005	#include <ctime>
006	#include <cstdlib>
007	using namespace std;
008	 
009	//function declarations
010	void init();
011	void menu();
012	void checkSeat();
013	void printTicket();
014	void displaySeating();
015	 
016	//global variables
017	const int firstClassPrice = 10000;
018	const int economyPrice = 5000;
019	const int rows = 9;
020	const int columns = 5;
021	const int size = 50;
022	char seat[rows][columns];
023	double price, discount;
024	string type, sname, name[rows][columns];
025	bool answer;
026	void init()
027	    {
028	        for ( int rows = 0; rows < 9; rows++ )
029	            for ( int columns = 0; columns < 5; columns++ )
030	                seat[rows][columns] = '0';
031	    }
032	     
033	void menu()
034	        {
035	            cout << "Welcome to taxiflite.com\n";
036	            cout << "Please choose one of the following:\n ";
037	            cout << "1. First Class\n";
038	            cout << "2. Economy\n";
039	            cout << "3. Seating allocation\n";
040	            cout << "4. Ticket holder\n";
041	        }
042	         
043	void checkSeat()
044	        {
045	            char reply;
046	     
047	            cout << "Enter name: ";
048	            cin >> sname;
049	             
050	            cout << "Do you want a specific seat:(y/n) : ";
051	            cin >> reply;
052	             
053	            if( reply == 'y' )
054	               {   
055	                init();
056	                cout << "Enter seat: \n";
057	                cin >> seat[rows][columns];
058	             
059	                if(seat[rows][columns] == 'X')
060	                  {    
061	                    seat[rows][columns] = 'X';
062	                    name[rows][columns] = sname;
063	                   }
064	                    
065	                else
066	                   {   
067	                    int x = rand()%rows;
068	                    int y = rand()%columns;
069	                    while(seat[x][y] == 'X');
070	                        {  
071	                            int x = rand()%rows;
072	                            int y = rand()%columns;
073	                        }
074	                         
075	                    seat[rows][columns] = 'X';
076	                    name[rows][columns] = sname;
077	                   }
078	               }       
079	            else
080	              {
081	                init();
082	                int rows = rand()%rows;
083	                int columns = rand()%columns;
084	                    while ( seat[rows][columns] = 'X')
085	                       {
086	                        int rows = rand()%rows;
087	                        int columns = rand()%columns;
088	                       }
089	                seat[rows][columns] = 'X';
090	                name[rows][columns] = sname;
091	              }
092	          }
093	           
094	        void printTicket()
095	            {
096	   
097	                cout <<"Name: " << name << endl;
098	                cout << "Seat No: " << seat << endl;
099	                cout << "Ticket type: " << type << endl;
100	                cout << "price: " << price << endl;
101	            }
102	             
103	        void displaySeating()
104	            {
105	            }
106	 
107	int main()
108	{
109	    int choice;
110	    srand(time(NULL)); //need this for random numbers
111	    for ( int count = 0 ; count < 50 ; count++)
112	    {  
113	        menu();
114	        cin >> choice;
115	        switch (choice)
116	       {
117	            case 1:
118	                type = "First class";
119	                init();
120	                while ( rows < 2 )
121	                   {
122	                    checkSeat();
123	                    bool isleSeat1 = seat[rows][1];
124	                    bool isleSeat2 = seat[rows][2];
125	                        if ( isleSeat1 == true ||  isleSeat2 == true )
126	                            {       
127	                                discount = 10 / 100 * firstClassPrice;
128	                                price = firstClassPrice - discount;
129	                            } 
130	                        else
131	                            {
132	                                price = firstClassPrice;
133	                            }
134	                   }
135	                break;
136	                    
137	            case 2:
138	                type = "Economy";
139	                init();
140	                while( rows > 2 )
141	                {
142	                    checkSeat();
143	                    bool isleSeat1 = seat[rows][1];
144	                    bool isleSeat2 = seat[rows][2];
145	                        if ( isleSeat1 == true ||  isleSeat2 == true )
146	                            {       
147	                                discount = 10 / 100 * firstClassPrice;
148	                                price = firstClassPrice - discount;
149	                            } 
150	                        else
151	                            {
152	                                price = firstClassPrice;
153	                            }
154	                   }
155	                break;
156	                    
157	             case 3:
158	                printTicket();
159	                break;
160	              
161	            case 4:
162	                 
163	            default:
164	                 cout << "Incorrect. Choose between 1-4 \n";
165	             
166	         
167	       }
168	    }
169	    return 0;
170	}
closed account (o3hC5Di1)
Hi there,

Glad to see you took the time to go through my corrections.

As for your questions:

first tyme wrote:
1. For my number 1 option in my switch statement, when i enter "1", nothing happens.


Your problem is on line 120:
while ( rows < 2 )
You have declared rows as such: int rows = 9; , so it will never be smaller than 2 unless you change it.


first tyme wrote:
2. when i ask clients their seat numbers they are going to enter something like "A1; B3", how do i do that


I would change your funciton init() to fill the row-numbers as letters, then you can easily look seats up that the user enters:

1
2
3
4
5
6
7
8
9
10
11
12
13
void init()
{
   char[10] letters = "ABCDEFGHIJ";
   char letter;	       
   for ( int row = 0; row < rows; row++ )
   {
       letter = letters[row]
	            for ( int column = 0; column < 5; column++ )
                   {
	                seat[letter][column] = '0';
                    }
   }
}


first tyme wrote:
3. how would you code something like, if economy is full and you ask if they want to go to First class and assign a seat for them there

Easiest would be to keep a counter of the number of economy seats that were booked and check against that if (economy_counter == MAX_ECONOMY) cout << "first class?"; Another possibility is to check the range of the economy class within your seats[][] array entirely for empty seats.

first tyme wrote:
4. does my code make sense??

It's not too bad... Some possible improvements:
- don't use for ( int count = 0 ; count < 50 ; count++) to keep your program going, rather make a 5th menu option "quit", and then wrap that code in a do { <code> } while (choice != '5');
- put the asking for input and processing of the input (switch) in the menu() function, it's generally wise to keep as much logic out of the main() function that doesn't need to be there.
- put the init() function in the main() function as it only needs to run once


Hope that helps.

All the best,
NwN
Last edited on

i dont understand how this works, my code wont compile
 
char[<number_of_rows>] letters = {'A', 'B', 'C', /*.... */};


can u please explain this too for me
 
letter = letters[rows]


thank you for everything u have done for me.
closed account (o3hC5Di1)
Hi there,

That code was just an example; what you need is

 
char letters[10] = "ABCDEFGHIJ";


So your for loop in the init() function will look like this (don't use this code, it's an example of how it works):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void init()
{
   char letters[10] = "ABCDEFGHIJ";
   char letter;	       
   /* while variable int row is smaller than our maximum amount of rows (10)*/
   for ( int row = 0; row < rows; row++ )
   {
        //assign the letter of the current row to letter variable;
        letter = letters[row]
                    //for every seat in a row
	            for ( int column = 0; column < 5; column++ )
                   {
	                //make seat['A'][0] for instance empty
                        seat[letter][column] = '0';
                    }
   }
}


It will make it easier for you because now you can ask your user for a seat number, for instance D4, and you can refer to it in your code as seats['D'][4].

Hope that makes sense to you :)

All the best,
NwN
Topic archived. No new replies allowed.