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.

This program can be done using either a single-dimensional array or a multi-dimensional array. Since, we only discussed
single-dimensional arrays in class, do this program using a single-dimensional array.

As with all your programs, be sure to include the standard header talked about in class in your program. Also, be sure to comment your program appropriately. Try to use functions/ procedures where applicable. Be sure to include headers on these functions/procedures when used.

Welcome to the site. For us to best help you with your assignment, please post all relevant code and any specific questions that you have.

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

using namespace std;

void initialize( char form[][6]);
void getData(char& ticketType, int& row, 
             char& column);
void printForm(char form[][6], int row, char column);

int main()
{
    char ch, ticketType, column;
    int row;
    char form[13][6];
    
    initialize( form);
    cout << "This program assigns seats for a plane.\n"
         << "Do you want to start now? Y/y for yes, N/n for no." << endl;
    cin >> ch;
    
    ch = static_cast<char>(toupper(ch));
    while(ch == 'Y')     
    { 

        getData(ticketType, row, column);
        printForm(form, row, column);
        
        cout << "This program assigns seats for a plane.\n"
             << "Do you want to start now? Y/y for yes, N/n for no." << endl;
        cin >> ch;
        ch = static_cast<char>(toupper(ch));
        if(ch == 'N')
          return 0;     
    }// end while   

    system("PAUSE");
    return 0;
}

void initialize( char form[][6])
{
     for(int i=0;i < 13;i++)
       for(int j=0;j<6;j++)
         form[i][j]='*';
}



void getData(char& ticketType, int& row, char& column)
{          
    cout << "The airplane has 13 rows, with  six seats in each row. " << endl;
           
    cout << "Enter ticket type,\n"
         << "F for first class, \n"
         << "B for business class,\n"
         << "E for economy class:" << endl;
    cin >> ticketType;
    ticketType = static_cast<char>(toupper(ticketType));
    while(ticketType != 'F' && ticketType != 'B' 
          && ticketType && ticketType != 'E')
    {
        cout << "Invalid ticket type." << endl;
        cout << "Enter ticket type,\n"
             << "F for first class, \n"
             << "B for business class,\n"
             << "E for economy class:" << endl;
        cin >> ticketType;
        ticketType = static_cast<char>(toupper(ticketType));
     }      
    switch(ticketType)
    {
           case 'F':
                cout <<  "Row 1 and 2 are first class,\n";
                break;
           case 'B':
                cout <<  "row 3 throuh 7 are business class,\n";
                break;
           case 'E':
                cout <<  "row 8 through 13 are economy class." << endl; 
                break;
    }// end switch
                
    cout << "Enter the row number you want to sit: " << endl;
    cin >> row;  
    
    cout << "Enter the seat number (from A to F). " << endl;
    cin >> column;
    column = static_cast<char>(toupper(column));  
    
}    

void printForm(char form[][6], int row, char column)
{
    int i, j;
  
    if(form[row-1][static_cast<int>(column-65)]=='X')
   {
       cout << "This seat already assigned. Choose another seat: " << endl;
       cin >> column;
       column = static_cast<char>(toupper(column));  
   }  
    form[ 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" << setw(6) << "E" << setw(6) << "F" << endl;
         
    for(i = 0; i < 13; i++)
    {
       cout << left << setw(3) << "Row " << setw(2)<< i+1;
       for(j = 0; j < 6; j++)
       {
          cout << right  << setw(6) << form [i][j];
       }
       cout << endl;
    }

}

Edited 7 Years Ago by Nick Evan: Added code-tags

please help me because i'm a beginner in this language

It looks pretty good sofar. It becomes a little unclear when you have the user attempt to retype only a column when the seat is occupied. I would recommend putting a return; (no value we just want to get out) after cout << "This seat already assigned. Choose another seat: " << endl; Once the flow returns back to main you'll go through the while loop again anyway so you can save the user the trouble of picking another seat in the same row especially if they are already occupied.

P.S. please use the //code goes here tags next time

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.