Code:
#include <stdio.h>
/* Declare Global Variables */
int airplane[11]; /* array to store the 10 seat values, seats 1 - 10 */
int section;
int choice = 0;
/* Define Functions */
/* function to display menu */
int menu(void)
{
do
{
printf( "********************************************************************* \n\n\n" );
printf( "Class/Section Menu \n\n" );
printf( "1 - First Class \n" );
printf( "2 - Loser Class \n\n" );
printf( "Please enter 1 for a First Class ticket or please enter 2 for a Loser Class ticket: " );
scanf( "%i", &choice );
/* choice has to be a 1 or a 2 */
if( choice < 1 || choice > 2 )
{
printf( "Invalid choice, please try again dummy!! \n" );
}
}
while ( choice < 1 || choice > 2 );
printf( "********************************************************************* \n\n\n" );
return(choice);
}
/* function to select seats in first class or loser class */
int SeatsAvailable ( int section )
{
int n;
int s;
int response;
int flag = 0;
switch ( section ) /* remember, no semi-colon here */
{
/* case 1 is where seats are selected for first class */
case 1:
printf( "\n\n" );
printf( "The following First Class seats are available for your selection: \t\n\n " );
for( n = 1; n < 6; ++n ) /* this for loop steps through seats 1 thru 5 */
{
/* this checks to make seat selected has not already been assigned, if does not equal 0, seat is assigned already */
if( airplane[n] == 0 )
{
printf( "%i", n );
}
else
{
++ flag; /* flag value is incremented every time a seat is selected in first class */
printf( "\n\n" );
/* the current flag value is a troubleshooting tool */
printf( "Current flag value: %i\n", flag );
printf( "\n" );
/* the seat value is a troubleshooting tool */
printf( " If seat value is a 1 - one, seat is already assigned, if seat value is a 0 - zero, seat is available \n\n" );
printf( "Seat values, \t seat1 %i \t seat2 %i \t seat3 %i \t seat4 %i \t seat5 %i \n\n",
airplane[1],
airplane[2],
airplane[3],
airplane[4],
airplane[5] );
}
}
if( flag == 5 ) /* flag is incremented until 5 is reached, no more available seats in respective class */
{
/* this is printed when there are no available first class seats */
printf( "No available First Class seats, do you want the lowly Loser Class? \t 1 = yes \t 0 = no" );
scanf( "%i", &response );
if( response )
{
/* this allows user to select a seat in loser class when no first class seats are available */
s = SeatsAvailable(2);
}
else
{
/* this is printed when they don't want to sit in loser class when there are no first class seats available */
printf( "You're screwed, there is another flight in 3 long hours, enjoy the long, long, long wait \n");
s = 0;
break; /* this break was added to go right to close plane when they need to go on another flight */
}
}
do
{
printf( "\n\n" );
printf( "Select your seat: \n\n" );
scanf( "%i", &s );
/* seat number has to be between 1 and 5 */
if( ( s < 1 ) || ( s > 6 ) || ( airplane[s] == 1 ) )
{
printf( "Invalid entry, please try again dummy!! \n" );
}
}
while ( ( s < 1 ) || ( s > 6 ) || ( airplane[s] == 1 ) );
break;
/* case 2 is where seats are selected for loser class */
case 2:
printf( "\n\n" );
printf( "The following Loser Class seats are available for your selection: \n\n" );
for( n = 6; n < 11; ++n ) /* this for loop steps through seats 6 thru 10 */
{
/* this checks to make seat selected has not already been assigned, if does not equal 0, seat is assigned already */
if( airplane[n] == 0 )
{
printf( "%i", n );
}
else
{
++ flag; /* flag value is incremented every time a seat is selected in first class */
printf( "\n\n" );
/* the current flag value is a troubleshooting tool */
printf( "Current flag value: %i\n", flag );
printf( "\n" );
/* the seat value is a troubleshooting tool */
printf( " If seat value is a 1 - one, seat is already assigned, if seat value is a 0 - zero, seat is available \n\n" );
printf( "Seat values, \t seat6 %i \t seat7 %i \t seat8 %i \t seat9 %i \t seat10 %i \n\n",
airplane[6],
airplane[7],
airplane[8],
airplane[9],
airplane[10] );
}
}
if( flag == 5 ) /* flag is incremented until 5 is reached, no more available seats in respective class */
{
/* this is printed when there are no available loser class seats */
printf( "No available Loser Class seats, do you want the Uptight First Class? \t 1 = yes \t \t 0 = no \t" );
scanf( "%i", &response );
if( response )
{
/* this allows user to select a seat in loser class when no first class seats are available */
s = SeatsAvailable(1);
}
else
{
/* this is printed when they don't want to sit in first class when there are no loser class seats available */
printf( "You're screwed, there is another flight in 3 long hours, enjoy the long, long, long wait \n");
s = 0;
break; /* this break was added to go right to close plane when they need to go on another flight */
}
}
do
{
printf( "\n\n" );
printf( "Select your seat \n\n" );
scanf( "%i", &s );
/* seat number has to be between 6 and 10 */
if( ( s < 6 ) || ( s > 11 ) || ( airplane[s] == 1 ) )
{
printf( "Invalid entry, please try again dummy!! \n" );
}
}
while ( ( s < 6 ) || ( s > 11 ) || ( airplane[s] == 1 ) );
break;
default: printf( " The End, Good Bye!! \n" );
break;
}
return(s);
}
/* this function prints out the boarding pass each time a seat is selcted & successfully assigned */
void BoardingPass( int a )
{
//airplane[seat] = a;
printf( "********************************************************************* \n" );
printf( "*************************** BOARDING PASS *************************** \n\n\n" );
if ( a <= 5 ) /* this prints a first class boarding pass */
{
printf( "Your seat number in First Class is: %i \n\n", &a ); /* does not print correct seat number */
printf( "IMPORTANT - You cannot pass through security, or board plane without this boarding pass \n\n\n" );
printf( "********************************************************************* \n" );
printf( "********************************************************************* \n\n\n" );
}
else
{
printf( "Your seat number in Loser Class is: %i \n\n", &a ); /* does not print correct seat number */
printf( "IMPORTANT - You cannot pass through security, or board plane without this boarding pass \n\n\n" );
printf( "********************************************************************* \n" );
printf( "********************************************************************* \n\n\n" );
}
}
int main(void)
{
int option;
int close;
int seat;
printf( "Welcome to the friendly skies of Island Hopper Airlines \n\n" );
do
{
option = menu();
seat = SeatsAvailable( option );
if( seat != 0 )
{
airplane[seat] = 1;
//printf( "Seat number: %i", airplane[seat] ); this always prints out seat = 1
printf( "\n\n" );
BoardingPass ( seat );
}
printf( "Are you done - close the plane? \t 0 = no \t \t 1 = yes \t" );
scanf( "%i", &close );
}
while( !close );
printf( "\n\n\n\n" );
}