C++/C++

Advertisement


Question
QUESTION: Write a program to assign seats on each flight of the airline's only plane (capacity:10 seats). 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 assign a seat in the first class section (seats 1-5). If the person types 2,the program should assign a seat in economy section (seats 6-10). The program should print a boarding pass indicating the persons seat number and weather it is in the first class or economy section of the plane. Use a single subscripted 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 1 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. If no then print the message "Next Flight leaves in 3 hours."

#include <iostream>
#include <string>

using namespace std;
// prototype

void menu(void);


int main()
{
         
         int seats[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
         
         char option;
     int seat;
     menu();

         cin >> option;

     if (option == '1' || option == '2')

        seat=CheckSeat (option);
     else
         return 0;
     if (seat  >= 0 && seat < 10)
        seats[seat]=1;
     else
        cout << "seat not available in " << opton << endl;


     if (option == '1' && seat < 6)  // firstclass
     {
         
         

         cout << "Your seat number is "<< seat + 1 <<" and you are in "First Class" " << endl;
     }
         else
         if (option == '2' && seat >=5 || seat < 10))
     {
         
         cout << "Your seat number is " << seat + 1 << "and you ar in Economy" << endl;
     ]


  return 0;
}
void menu(void)
{
  cout >>"please type 1 for first class" << endl;
  cout >>"please type 2 for economy" << endl;
  cout >>"please enter 9 to not book a flight" << endl;
}
int checkseat(char selection,int seats[])
{
  if(selection == '1')
  {   
  for(int index=0; index<5;index++)
     if (seats[index] == 0)
        return index;
  return -1
  }
  else
     if(selection == '2')
     {
     for(int index=5; index<10; index++)
        if(seats [index] == 0)
        return index;
     }
     return -1;
}
I have this so far but i cant seem to get it to work.

ANSWER: It looks pretty good - I don't know what you mean by "cant seem to get it to work".  It's not close to being done - you have to offer the other seating area if one section is full, you have to "print a boarding pass" (is that literal meaning to a printer?)  This isn't a small task - did you start with pseudo-code to describe what you have to do for all the branches of the program?  This will help you to organize what the functions have to do.

Bill

---------- FOLLOW-UP ----------

QUESTION: I dont know what to do next.Could you assist me on the next piece to offer the other seating area if one section is full. No it dont mean literal to a printer

Answer
CheckSeat returns -1 when there are no seats.  I'm OK with this decision.

When it returns -1, you know the section (option).  Use CheckSeat again with the other option - if this call returns -1, you have no seats.  If it doesn't, tell the user the other section be being assigned and assign that seat.  The following code shows how this 3 sentence description is coded:

create an array outside of main for section titles in printf:

const char * const section[] = { "First class", "Economy" };

Code to take option (valid has to be '1' or '2'):

if (option == '1' || option == '2')
 {
 seat=CheckSeat (option);
 if (seat == -1)
   {
   int previousOption = option;
   if (option == '1')  // switch sections
     option = '2';
   else
     option = '1';
   seat=CheckSeat (option);  // Check the other section
   if (seat != -1)
     printf ("Section %s is full.  Assigning seat in section %s\n", section[previousOption-'1'], section[option-'1']);
   }
 }
if (seat != -1)
 printf("Your seat assignment is seat %d in section %s\n", seat, section[option-'1'])
else
 printf("The airplane is full!\n");

Notice how it falls through to the same code for either an opening in the desired section as well when the other section is switched.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.