📄 airlinereservation.cpp
字号:
// Exercise 13.11 AirlineReservation.cpp
// Application that enables a user to reserve a seat on a flight
// and displays a boarding ticket.
#include <iostream> // required to perform C++ stream I/O
using namespace std; // for accessing C++ Standard Library members
// function main begins program execution
int main()
{
// define variables and create array to represent seats
int choice; // seat number that the user selects
char response; // whether user will take a different seat
// continue until plane is full
while ( people < ( SEATS - 1 ) ) {
// display menu of options
cout << "\nSelect one of the following options" << endl
<< "1 - \"First class\"" << endl
<< "2 - \"Economy\"" << endl
<< "? ";
cin >> choice; // get user's choice
// if user selects first class
if ( choice == 1 )
{
// if there are seats available, assign a seat
if ( !plane[ firstClass ] && firstClass <= 5 )
{
cout << "\nYour seat assignment is " << firstClass
<< " in the first class section." << endl;
// indicate that seat is taken, then increment firstClass
} // end if
// if no first class seats, but economy seats are available
// ask if passenger would like to sit in economy section
else if ( firstClass > 5 && economy <= 10 )
{
cout << "\nThe first class section is full.\n"
<< "Would you like to sit in the economy"
<< " section (Y or N)? ";
cin >> response; // store response
// if economy is suitable, assign seat in economy section
if ( response == 'Y' || response == 'y' )
{
cout << "\nYour seat assignment is " << economy
<< " in the economy section." << endl;
// indicate that seat is taken, then increment people
} // end if
// if economy seat not suitable display next departure
else
{
cout << "\nNext flight leaves in 3 hours." << endl;
} // end inner else
} // end outer else
// if no economy seats are available, display next departure
else
{
cout << "\nNext flight leaves in 3 hours." << endl;
} // end else
} // end outer if
// if user selects economy
else if ( choice == 2 )
{
// if seats are available, assign one
if ( !plane[ economy ] && economy <= 10 )
{
cout << "\nYour seat assignment is " << economy
<< " in the economy section." << endl;
// indicate that seat is taken, then increment people
} // end if
// if only first class seats are available,
// ask if first class is suitable and assign seat
else if ( economy > 10 && firstClass <= 5 )
{
cout << "\nThe economy section is full." << endl
<< "Would you like to sit in the first class"
<< " section (Y or N)? ";
cin >> response; // store response
if ( response == 'Y' || response == 'y')
{
cout << "\nYour seat assignment is " << firstClass
<< " in the first class section.\n";
// indicate that seat is taken,
// then increment firstClass
} // end if
// if first class not suitable, print next departure
else
{
cout << "\nNext flight leaves in 3 hours.\n";
} // end inner else
} // end inner else if
// if no seats left, print next departure
else
{
cout << "\nNext flight leaves in 3 hours.\n";
} // end else
} // end else if
// otherwise, user entered invalid number
else
{
cout << "\nError: You entered an invalid number. "
<< "Please enter 1 or 2." << endl;
} // end outmost else
} // end while
cout << "\nAll seats for this flight are sold." << endl << endl;
return 0; // indicates successful termination
} // end function main
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -