Basic program to assign passengers to airplane seat

I'm a beginner to C++, and am really struggling with the current project. I've emailed to professor, but she isn't the most helpful and I'm taking the class online, so unfortunately my understanding is solely what I've gotten from the textbook...


The part I'm stuck on is to create a program that will allow a user to assign passengers to a specific seat on an airline. The layout of the plane is 9 rows (which are numbered), with seats A-D. There should be function call in the main that displays a layout of the plane's seats, then the user is prompted to input a row # and seat letter (which I'm converting to a value by subtracting 65- this does work!). If the seat is available, an 'X' is marked in its place on the layout. If the seat is already filled, a message is output. The layout should be displayed before the user enters the row/seat so they can see if their selected seat is available before entering it. If -1 is input for the row, the program ends.

I got something using a string to run, but my professor said I needed to do it using her specific array, which I don't understand, and now can't get to run. I'm honestly not even sure how to plan this out. I tried to outline it a bit in my program, but I could really use some pointers since I really don't know how to start.



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

// constant values
const int ROW = 9;
const int COL = 4;
int row, seat;

// list of functions
void displayPlane (char layout[ROW][COL]);
int getSeats (istream row, ostream seat);

int main ()
{

// initial plane layout
char layout[ROW][COL] = {  { 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' }};

// call function to getSeats (which will also displayPlane)
// I have no idea how to do this part... It's supposed to be a call by reference I think, but I'm lost as to how to set the function call. 
istream row;
ostream input_col;

getSeats(istream row, ostream seat);

}

// this is what my professor sent me and said I'm required to use, but I don't understand. 
void displayPlane(char layout[ROW][COL])

{
cout << endl << "     Chesapeake Airlines" << endl << endl;

for (int i = 0; i < ROW; i++) {

        cout << "    " << i + 1 << " ";

for (int j = 0; j < COL; j++) {

            cout << "  " << layout[i][j] << " ";

        }

        cout << endl;

    }

   

return;

}


/*

I'm also supposed to use this to check if the seat is taken, which makes sense, but she said to put this part in the main?

if(layout[r][c] != 'X') 

This was how she said I need to count the total seats sold. There are three different classes, so I think that's
the part in the curly brackets, but I don't get how the counter actually works. 

int classCtr[CTR] = {0,0,0}; */
do you need to deal with file input/output ?
// this is what my professor sent me and said I'm required to use, but I don't understand.
void displayPlane(char layout[ROW][COL])

displayPlane will print out your seating array.

like so...
1
2
3
4
5
1 A B C D
2 A X C D
3 X X X D

etc...


where did you get the idea for the following code, it's all file related.
1
2
3
4
istream row;
ostream input_col;

getSeats(istream row, ostream seat);



the counters are easy to manage, lets assume they are 1st 2nd and 3rd class in that order. Of course, you will need to know which class the seats are so you know which counter to increment.
1
2
classCtr[0]++; // add 1 to 1st class counter
classCtr[1]++; // add 1 to 2nd class counter 


have you tried any psuedo code? like this... Its just an example but it helps you organise your thoughts about what needs to happen when.

1
2
3
4
5
6
7
8
9
10
loop
   display seat list
   input seat choice
   if seat choice valid then
      mark seat with X
      add to seat counter
   else
      report error to user
   endif
end loop



Okay, so I've gotten a little further. Jaybob, your pseudocode is basically what I was aiming for, but hadn't gotten to work. So I think I've followed that now, but there's something wrong with my loop. It doesn't change the seats to X's, and doesn't seem to ask things in order?

The file-type format was suggested by a friend; it didn't make sense, but thought I'd try it. I think I have the function calls correct now. Thanks!

ETA: I've spent some more time messing around with different parts, and I'm pretty sure my problem is somewhere in lines 34-47. It runs, but the layout doesn't change, and it doesn't always allow the user to input a seat after the first iteration.

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

// constant values
const int ROW = 9;
const int COL = 4;
int row, seat;

// list of functions
void displayPlane (char layout[ROW][COL]);
int getSeats (int &seat);

int main ()
{

// initial plane layout
char layout[ROW][COL] = {  { 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
	                        { 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' },
							{ 'A', 'B', 'C', 'D' }};

// call function to getSeats (which will also displayPlane)

	cout << "\nEnter row <-1 to stop>: ";
	cin >> row;

while (row != -1)
	{
	displayPlane(layout);
	getSeats(seat);
	cout << "\nEnter row <-1 to stop>: ";
	cin >> row;
	system ("pause");
	}
// this should change the different input seats to X's, but doesn't? 
if (layout[row][seat] != 'X')
		layout[row][seat] = 'X';
	else 
		cout << "Seat is unavailable.";

}

 int getSeats(int &seat)
 {
int seatInput;
	cout << "Enter seat: ";
	cin >> seatInput; 
	seat= seatInput - 65;
	return seat;
 }

	



void displayPlane(char layout[ROW][COL])

{
cout << endl << "    Chesapeake Airlines" << endl << endl;

for (int i = 0; i < ROW; i++)
  {
        cout << "    " << i + 1 << " ";
for (int j = 0; j < COL; j++)  {
            cout << "  " << layout[i][j] << " ";  }
        cout << endl;
    }

   

return;

}
Last edited on
Line 31,32 aren't needed. You just need to initialise row to be something other than -1 so that it enters the loop. or use the do { } while(); loop instead;

@line 41, you close the loop, before you process the seat, in general, always indent your code properly, it will highlight these problems to you.

looking better though :)
Last edited on
» try to allocate chars for char layout[ROW][COL] programmatically.
» why do you need system ("pause"); at line 40?
» getSeats function is wrong. you need a row value and a column value.
Heh im in the same class as you :p CCBC?
Topic archived. No new replies allowed.