#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;

const int NUMROWS = 14;
const int NUMSEATS = 7;
//enum seatsType {A,B,C,D,E,F};

void initPlane(char plane[NUMROWS][NUMSEATS])
{
     int x,y;
     //strcpy(plane[2], "Row 1");
     plane[0][0] = ' ';
     plane[0][1] = 'A';
     plane[0][2] = 'B'; 
     plane[0][3] = 'C';
     plane[0][4] = 'D';
     plane[0][5] = 'E';
     plane[0][6] = 'F';
     plane[1][0] = '1';
     plane[2][0] = '2';
     plane[3][0] = '3';
     plane[4][0] = '4';
     plane[5][0] = '5';
     plane[6][0] = '6';
     plane[7][0] = '7';
     plane[8][0] = '8';
     plane[9][0] = '9';
     plane[10][0] = 'W';
     plane[11][0] = 'Y';
     plane[12][0] = 'Z';
     plane[13][0] = 'G';
     for (x = 1; x < NUMROWS ; x++)
     {
         for ( y = 1 ; y < NUMSEATS ; y++)
         plane[x][y] = '*';
     }
}
// POSTCONDITION: 
//   plane[x][0] == 'A', 0<=x<13
//   plane[x][1] == 'B', 0<=x<13
//   plane[x][2] == 'C', 0<=x<13
//   plane[x][3] == 'D', 0<=x<13


void printPlane(char msg[], char plane[NUMROWS][NUMSEATS])
{
     cout << msg << endl;
     int Row,col;
     for ( Row = 0; Row < NUMROWS ; Row ++)
     {
         for (col = 0; col < NUMSEATS; col++)
         cout << setw(5) << plane [Row][col] << " ";
         cout << endl;
     }
}
// POSTCONDITION: The seating layout of the plane has been printed
// to the standard output with an X displayed for each taken seat.


int main()
{
  int seatsTaken = 0;
  int seats = NUMROWS * NUMSEATS;
  char plane[NUMROWS][NUMSEATS];
  char keepGoing = 'y';
  int row = 0; 
  char seat;
  int rowIndex, seatIndex;
  char ticketType[15];

   initPlane(plane);

  cout << "Choose your seat!" << endl;
  while (seatsTaken < seats && keepGoing == 'y')
    {
      //
      // Show layout and get seat choice
      // 
      printPlane("Plane layout; X designates taken seats", plane);
      cout << "Row 1 and 2 are first clss. " << endl;
      cout << "Rows 3 through 7 are business class. " << endl;
      cout << "Rows 8 through 13 are economy class. " << endl;
      cout << "Eneter ticket type (first class,business class,or economy class) :" ;
      cin >> row >> seat;
           

      
      // Adjust input to use as indices
      
      rowIndex = row;
      
      
      if ( seat == 'A' || seat == 'a' )
      seatIndex = 1;
      else if ( seat == 'B' || seat == 'b' )
      seatIndex = 2;
      else if ( seat == 'C' || seat == 'c' )
      seatIndex = 3;
      else if ( seat == 'D' || seat == 'd' )
      seatIndex = 4;
      else if ( seat == 'E' || seat == 'e' )
      seatIndex = 5;
      else if ( seat == 'F' || seat == 'f' )
      seatIndex = 6;
      
      

      //
      // Check to see if seat is taken
      // 
      if (plane[rowIndex][seatIndex] == 'X')
	cout << "Sorry, " << row << seat << " is already taken." << endl;
      else
	{
	  cout << "OK, you've got " << row << seat << endl;
	  plane[rowIndex][seatIndex] = 'X';
	  seatsTaken++;
	}

      //
      // If there are seats left, check we should keep going
      // 

      if (seatsTaken < seats)
	{
	cout << "Choose another seat? (y/n) ";
	cin >> keepGoing;
	}
      else
	cout << "Plane is now full!" << endl;
    }

    printPlane("Final seating chart", plane);
    
    return 0;
    
}

here is the problem:
airplane seating assignment:
write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row,row 1 to 2 are first class,row 3 through 7 are business class and row 8 to 13 are economy class. your program must prompt the user to enter the following information:
a. Ticket type (first class,business class,economy class)
b. Desired seat
output the seating plan in the following form:
A B C..........F
Row 1 * * * *
Row 2 * X * *
Row 3 * * * *
Row 4 * * X *
row 13

here * indicates the seat available, X indicates the seat occupied . Make a menu driven program ; the show the user the choises and allow to make appropriate choices.


i was able to make the chart but i am not sure how to display Row 1 Row 2 ....Row 13 in the first colum of the array that was my first trouble so i thought i should move forward with giving the choices to make a ticket type selection but that also didn't work that well so if anyone can help me i would highly appreciate it ...thanxx

Attachments ab.jpg 45.39 KB

Use a loop to control each row to be printed by using a counter that is increased by 1 each time through the loop. Then within the body of the loop use an output statement to display the word ROW and the row number (which is actually directly related to the counter controlling the row being printed, but what that relation is will depend on how you set up the loop counter). Then embed a second loop within the outer loop to output the elements of each line from the array called plane. Then go to the next line and repeat until all lines are printed.

Use a loop to control each row to be printed by using a counter that is increased by 1 each time through the loop. Then within the body of the loop use an output statement to display the word ROW and the row number (which is actually directly related to the counter controlling the row being printed, but what that relation is will depend on how you set up the loop counter). Then embed a second loop within the outer loop to output the elements of each line from the array called plane. Then go to the next line and repeat until all lines are printed.

actually i tried that already but there was a problem with that my output was going one digit of balance when my rows became two digit like row 10 . so i found a way out its kind of a stupid though my new code looks like this

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;

const int NUMROWS = 14;
const int NUMSEATS = 7;


void initPlane(char plane[NUMROWS][NUMSEATS])
{
     int x,y;
     //strcpy(plane[2], "Row 1");
     plane[0][0] = ' ';
     plane[0][1] = 'A';
     plane[0][2] = 'B'; 
     plane[0][3] = 'C';
     plane[0][4] = 'D';
     plane[0][5] = 'E';
     plane[0][6] = 'F';
     plane[1][0] = ' ';
     plane[2][0] = ' ';
     plane[3][0] = ' ';
     plane[4][0] = ' ';
     plane[5][0] = ' ';
     plane[6][0] = ' ';
     plane[7][0] = ' ';
     plane[8][0] = ' ';
     plane[9][0] = ' ';
     plane[10][0] = '\0';
     plane[11][0] = '\0';
     plane[12][0] = '\0';
     plane[13][0] = '\0';
     for (x = 1; x < NUMROWS ; x++)
     {
         for ( y = 1 ; y < NUMSEATS ; y++)
         plane[x][y] = '*';
     }
}
// POSTCONDITION: 
//   plane[x][0] == 'A', 0<=x<13
//   plane[x][1] == 'B', 0<=x<13
//   plane[x][2] == 'C', 0<=x<13
//   plane[x][3] == 'D', 0<=x<13


void printPlane(char msg[], char plane[NUMROWS][NUMSEATS])
{
     cout << msg << endl;
     int Row,col;
     for ( Row = 0; Row < NUMROWS  ; Row ++)
     {
         if (Row == 0)
         cout << "     " ;
         else if (Row != 0) 
         cout << "ROw " << Row;
         if (Row < 10 )
         cout << " ";
         for (col = 0; col < NUMSEATS; col++)
         cout << setw(2) << plane [Row][col] << " ";
         cout << endl;
         
     }
}
// POSTCONDITION: The seating layout of the plane has been printed
// to the standard output with an X displayed for each taken seat.


int main()
{
  int seatsTaken = 0;
  int seats = NUMROWS * NUMSEATS;
  char plane[NUMROWS][NUMSEATS];
  char keepGoing = 'y';
  int row = 0; 
  char seat;
  int rowIndex, seatIndex;
  char ticketType[15];

   initPlane(plane);

  cout << "Choose your seat!" << endl;
  while (seatsTaken < seats && keepGoing == 'y')
    {
      //
      // Show layout and get seat choice
      // 
      printPlane("Plane layout; X designates taken seats", plane);
      cout << "Row 1 and 2 are first clss. " << endl;
      cout << "Rows 3 through 7 are business class. " << endl;
      cout << "Rows 8 through 13 are economy class. " << endl;
      cout << "Eneter ticket type (first class,business class,or economy class) :" ;
      cin >> row >> seat;
        
      // Adjust input to use as indices
      
      rowIndex = row;
      
      
      if ( seat == 'A' || seat == 'a' )
      seatIndex = 1;
      else if ( seat == 'B' || seat == 'b' )
      seatIndex = 2;
      else if ( seat == 'C' || seat == 'c' )
      seatIndex = 3;
      else if ( seat == 'D' || seat == 'd' )
      seatIndex = 4;
      else if ( seat == 'E' || seat == 'e' )
      seatIndex = 5;
      else if ( seat == 'F' || seat == 'f' )
      seatIndex = 6;
      
      

      //
      // Check to see if seat is taken
      // 
      if (plane[rowIndex][seatIndex] == 'X')
	cout << "Sorry, " << row << seat << " is already taken." << endl;
      else
	{
	  cout << "OK, you've got " << row << seat << endl;
	  plane[rowIndex][seatIndex] = 'X';
	  seatsTaken++;
	}

      //
      // If there are seats left, check we should keep going
      // 

      if (seatsTaken < seats)
	{
	cout << "Choose another seat? (y/n) ";
	cin >> keepGoing;
	}
      else
	cout << "Plane is now full!" << endl;
    }

    printPlane("Final seating chart", plane);
    
    return 0;
    
}

now my problem is how to control the input like for diffrent classes of tickets like business class and econmy etc. and also how to control if its valid input or not cause if i try with my program 14 f it allocates that seat when actually it dosent even exist

int Row, col;
for ( Row = 1; Row < NUMROWS ; Row++)
{ 
   cout << "ROw " << Row << ' ';
   for (col = 1; col < NUMSEATS; col++)
       cout << plane [Row][col] << " ";
   
    cout << endl;
}

C/C++ assumes input is valid. If you don't trust the user to do that and you want to protect the program from doing something you don't want it to, then you have to program in data validation. In this case you can use a loop to control input.

bool needInput = true;
while(needInput)
{
   cout << "enter data" << endl;
   cin >> data;
   if(data > x && data < y)
     needInput = false;
}

thanx for the help the program is working fine and input validation was one gr8 idea i m going to keep hold of that thanx for the help again

thanx for the help the program is working fine and input validation was one gr8 idea i m going to keep hold of that thanx for the help again

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;

const int NUMROWS = 14;
const int NUMSEATS = 7;


void initPlane(char plane[NUMROWS][NUMSEATS]);
void printPlane(const char msg[], char plane[NUMROWS][NUMSEATS]);
void  checkType( char  &ticketType );
void typeCheck( char ticketType ,int &row, char &seat );

int main()
{
  int seatsTaken = 0;
  int seats = (NUMROWS - 1) * (NUMSEATS - 1);
  char plane[NUMROWS][NUMSEATS];
  char keepGoing = 'y';
  int row = 0; 
  char seat;
  int rowIndex, seatIndex;
  char ticketType;

   initPlane(plane);

  cout << "Choose your seat!" << endl;
  while (seatsTaken < seats && keepGoing == 'y')
    {
      //
      // Show layout and get seat choice
      // 
      printPlane("Plane layout; X designates taken seats", plane);
      cout << endl;
      
      cout << "\n" ;
      ticketType = toupper(ticketType);
      checkType ( ticketType);
      typeCheck( ticketType,row,seat);
     
      // Adjust input to use as indices
      
      rowIndex = row;
      
      seatIndex = toupper(seat) - 'A' + 1;
      
      /*if ( seat == 'A' || seat == 'a' )
      seatIndex = 1;
      else if ( seat == 'B' || seat == 'b' )
      seatIndex = 2;
      else if ( seat == 'C' || seat == 'c' )
      seatIndex = 3;
      else if ( seat == 'D' || seat == 'd' )
      seatIndex = 4;
      else if ( seat == 'E' || seat == 'e' )
      seatIndex = 5;
      else if ( seat == 'F' || seat == 'f' )
      seatIndex = 6;*/
      
      

      //
      // Check to see if seat is taken
      // 
      if (plane[rowIndex][seatIndex] == 'X')
	cout << "Sorry, " << row << seat << " is already taken." << endl;
      else
	{
	  cout << "OK, you've got " << row << seat << endl;
	  plane[rowIndex][seatIndex] = 'X';
	  seatsTaken++;
	}

      //
      // If there are seats left, check we should keep going
      // 

      if (seatsTaken < seats)
	{
	cout << "Choose another seat? (y/n) ";
	cin >> keepGoing;
	}
      else
	cout << "Plane is now full!" << endl;
    }

    printPlane("Final seating chart", plane);
    
    return 0;
    
} 
 void initPlane(char plane[NUMROWS][NUMSEATS])
{
     int x,y;
     plane[0][0] = ' ';
     for (x = 1; x < NUMROWS; x++)
     {
         plane[0][x] = 'A' + (x - 1);
     }
     for (x = 1; x < NUMROWS; x++)
     {
         for ( y = 0 ; y < 13 ; y++)
         plane[x][y] = ' ';
         }
    
     for (x = 1; x < NUMROWS ; x++)
     {
         for ( y = 1 ; y < NUMSEATS ; y++)
         plane[x][y] = '*';
     }
}
void printPlane(const char msg[], char plane[NUMROWS][NUMSEATS])
{
     cout << msg << endl;
     int Row,col;
     for ( Row = 0; Row < NUMROWS  ; Row ++)
     {
         if (Row == 0)
         cout << "     " ;
         else if (Row != 0) 
         cout << "ROw " << Row;
         if (Row < 10 )
         cout << " ";
         for (col = 0; col < NUMSEATS; col++)
         cout << setw(2) << plane [Row][col] << " ";
         cout << endl;
         
     }
}
// to the standard output with an X displayed for each taken seat.
void  checkType( char  &ticketType )
{
     
      bool needInput = true;
      while(needInput)
      {
                      cout << "Row 1 and 2 are first clss. " << endl;
      cout << "Rows 3 through 7 are business class. " << endl;
      cout << "Rows 8 through 13 are economy class. " << endl << "\n";
      cout << "Enter ticket type " << endl
           <<"Enter 'F'  for first class " << endl
           << "Enter 'N' for Economy non smoking class " <<endl
           <<"Enter 'S' Economy smoking class  :" ;
      cin >> ticketType;
      cout << "\n";
      ticketType = toupper(ticketType);
      if (ticketType == 'F')
      needInput = false;
      else if (ticketType == 'N')
      needInput = false;
      else if (ticketType == 'S')
      needInput = false;
      else
      {
          needInput = true;
          cout << "Invalid input. try again " << endl;
          cout << "\n";
           }
      }
      }
void typeCheck( char ticketType ,int &row, char &seat )
{
     bool needInput;
     if ( ticketType == 'F')
    {  needInput = true;
  while(needInput)
  {
     cout << "Enter the row(1-2) and seat(A-F) you would like (e.g., 3D): ";
                  cin >> row >> seat;
                  cout << "\n";
                  seat = toupper(seat);
                  if ((row >= 1) && (row <= 2))
                  {
                           
                           
                             if( seat == 'A')
                             needInput = false;
                             else if ( seat == 'B')
                             needInput = false;
                             else if ( seat == 'C')
                             needInput = false;
                             else if ( seat == 'D')
                             needInput = false;
                             else if (seat == 'E' )
                             needInput = false;
                             else if ( seat == 'F')
                             needInput = false;
                             else
                             {needInput = true;
                             cout << "Invalid input. try again " << endl;}
                             cout << "\n";
                              }
                              else 
                              {cout << "Invalid input. try again " << endl;
                              cout << "\n";}
                  }
                  }
          else if ( ticketType == 'N')    
           { needInput = true;
  while(needInput)
  {
     cout << "Enter the row(3-7) and seat(A-F) you would like (e.g., 3D): ";
                  cin >> row >> seat;
                  cout << "\n";
                  seat = toupper(seat);
                  if ((row >= 3) && (row <= 7))
                  {
                           
                           
                             if( seat == 'A')
                             needInput = false;
                             else if ( seat == 'B')
                             needInput = false;
                             else if ( seat == 'C')
                             needInput = false;
                             else if ( seat == 'D')
                             needInput = false;
                             else if (seat == 'E' )
                             needInput = false;
                             else if ( seat == 'F')
                             needInput = false;
                             else
                             {needInput = true;
                             cout << "Invalid input. try again " << endl;}
                              cout << "\n";}
                              else 
                              {cout << "Invalid input. try again " << endl;
                              cout << "\n";}
                  } 
                  }
                  else if ( ticketType == 'S')
                  {needInput = true;
  while(needInput)
  {
     cout << "Enter the row(8-13) and seat(A-F) you would like (e.g., 3D): ";
                  cin >> row >> seat;
                  cout << "\n";
                  seat = toupper(seat);
                  if ((row >= 8) && (row <= 13))
                  {
                           
                           
                             if( seat == 'A')
                             needInput = false;
                             else if ( seat == 'B')
                             needInput = false;
                             else if ( seat == 'C')
                             needInput = false;
                             else if ( seat == 'D')
                             needInput = false;
                             else if (seat == 'E' )
                             needInput = false;
                             else if ( seat == 'F')
                             needInput = false;
                             else
                             {needInput = true;
                             cout << "Invalid input. try again " << endl;}
                              cout << "\n";}
                              else 
                             {cout << "Invalid input. try again " << endl;
                             cout << "\n";}
                  } 
                  }
                  
                  }

and yay it works but one tinky winky glitch in there i am adding 1 to a char is that leagal with all the compilers

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