This is the problem

Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows:

A 1 2 3 4 5
B 1 2 3 4 5
C 1 2 3 4 5
D 1 2 3 4 5
E 1 2 3 4 5

The program should display the seat pattern with an 'X' marking the seats already assigned. For example, after seats 1A, 2B and 4C are taken, the display should look like this:

A X 2 3 4 5
B 1 X 3 4 5
C 1 2 3 X 5
D 1 2 3 4 5
E 1 2 3 4 5

After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice.

Except you have to use a dynamic array and the program is supposed to ask the user how many rows the plane has and then handles that many rows.

I can't figure out how to show the seats at the end...

``````#include<iostream>
using namespace std;

int main()
{
char ans, column;
int rows;
char seats[7][4];
do {

cout << "Enter the number of rows the plane has: " << endl;
cin >> rows;
char *p[4];
cout << "Enter the seat number you wish to sit in (A to D): " << endl;
cin >> column;
for (int i=0; i<4; i++)
p[i] = new char[rows];
for (int i=0; i<rows; i++)
{	for (int j=0; j<4; j++)
{	cout << p[j][i] << "   ";
}
cout << endl;
}
//Display seats

cout << "Do you want to add another seat? (Y or N)" << endl;
cin >> ans;
}while(ans == 'Y' || ans == 'y');
return 0;
}``````
4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by mokia1

Each row has 5 seats, but you don't know how many rows there will be until runtime. So the first thing you need to do is to declare a 2d array with unknown number of rows and columns, like this: `char **rows = NULL;` Now after asking how many rows the plane has then you can allocate the number of rows and seats in each row

``````const int SEATS = 5;
int nRows = 0;
cout << "Enter number of rows\n";
cin >> nRows;
char** rows = new char*[nRows];
for(int i = 0; i < nRows; i++)
{
rows[i] = new char[SEATS+1];
strcpy( rows, "12345" );
}``````

Now you are ready to display the available seats. When someone selects a seat, just replace the number in the row with an 'X' to indicate the seat has been taken.

``````#include <iostream>
using namespace std;

int main()
{
char ans, column;
int rows;
char seats[7][4];
do {

std::cout << "Enter the number of rows the plane has: " << endl;
cin >> rows;
char *p[4];
std::cout << "Enter the seat number you wish to sit in (A to D): " << endl;
cin >> column;
for (int i=0; i<4; i++)
p[i] = new char[rows];
for (int i=0; i<rows; i++)
{	for (int j=0; j<4; j++)
{	cout << p[j][i] << "   ";
}
cout << endl;
}
//Display seats

std::cout << "Do you want to add another seat? (Y or N)" << endl;
cin >> ans;
}while(ans == 'Y' || ans == 'y');
return 0;
}``````

I compiled and works, just dont recognise seats!

I know it compiles correctly just doesn't recognize the seats.. That is the point of this post!! I need help doing this!

please can't somebody help me with the DISPLAYING OF THE SEATS

You have the bit for displaying the seats right in the code, right in the for loop, it's just that you didn't assign them when the user entered them. You also didn't check the array to make sure that seat wasn't already assigned.

Your seats array should have 5 as the last number, not 4 because you have ABCDE (which have indicies 0 to 4). So you can declare your array up top as being size [][5] assuming it's acceptable to create a dynamic array that's only really half dynamic.

You know, you didn't need to post twice in 10 minutes to get attention to your post. Please be patient. I don't want to speak for anyone else, but you did get some help and then just kinda shrugged it off rather than returning with more code.

This is what I have.. it's actually four rows my bad..
I have it so it goes on the new number of rows that is input.. But now it doesn't let me choose which row to place a seat, but automatically sits me in the row number I typed for max rows. How can I solve this problem?

``````#include<iostream>
#include<cctype>
#include<iomanip>

using namespace std;

void initialize( char seats[][4]);
void getData(int& row, char& column);
void printForm(char seats[][4], int row, char column);

int main()
{
char ans, column;
int row;
char seats[7][4];

initialize(seats);
cout << "This program assigns seats for a plane.\n"
<< "Do you want to start now? (Y or N)" << endl;
cin >> ans;

while(ans == 'Y' || ans == 'y')
{

getData(row, column);
printForm(seats, row, column);

cout << "Do you want to add another seat? (Y or N)" << endl;
cin >> ans;
if(ans == 'N' || ans == 'n')
return 0;
}
return 0;
}

void initialize( char seats[][4])
{
for(int i=0; i<7; i++)
for(int j=0; j<4; j++)
seats[i][j]='*';
}

void getData(int& row, char& column)
{
char seats[7][4];
cout << "Enter the number of rows the plane has: " << endl;
cin >> row;
char *p[4];
cout << "Enter the seat number you wish to sit in (A to D): " << endl;
cin >> column;
for (int i=0; i<4; i++)
p[i] = new char[row];
for (int i=0; i<row; i++)
{	for (int j=0; j<4; j++)
{	seats[i][j]='*';
}
cout << endl;
//PROBLEM AREA	cout << "Enter the row you wish to sit in: " << endl;
//PROBLEM AREA	cin >> row;
}
}

void printForm(char seats[][4], int row, char column)
{
int i, j;

if(seats[row-1][static_cast<int>(column-65)]=='X')
{
cout << "This seat is already assigned. Choose another seat: " << endl;
cin >> column;
column = static_cast<char>(toupper(column));
}
seats[ row-1 ] [static_cast<int>(column)-65]= 'X';

cout << "* indicates that the seat is available; " << endl;
cout << "X indicates that the seat is occupied. " << endl;
cout << setw(12) << "A" << setw(6) << "B" << setw(6) << "C"
<< setw(6) << "D" << endl;

for(i = 0; i < row; i++)
{
cout << left << setw(3) << "Row " << setw(2)<< i+1;
for(j = 0; j < 4; j++)
{
cout << right  << setw(6) << seats [i][j];
}
cout << endl;
}

}``````

Edited by mokia1: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.