uuuu yeah, Salem is absolutely correct. And Salem knew that cut and pest is the reason.
Code:
/* Airline Reservation System */
#include <stdio.h>
#define SIZE 10
void reserve_first_class(int [], int); /* books first class ticket */
void reserve_economy(int [], int); /* books economy ticket */
void display_all(int [], int);
int main(void)
{
/* Declare arrays of seats and initialize all to 0 */
int seats[SIZE] = { 0 };
int choice;
do{
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Please type 3 to see all seats\n");
printf("Enter -99 to end>>>");
scanf("%d", &choice); /* Get choice from user */
switch(choice)
{
case 1:
/* User chose first class */
/* first class function call */
if(seats[SIZE/2-1] != 0 && seats[SIZE-1] != 0) //Check if at least a seat is available
{
printf("\n\nSorry the flight is currently full\n\n");
break;
}
reserve_first_class(seats, SIZE);
break;
case 2: /* User chose economy */
/* economy function call */
if(seats[SIZE/2-1] != 0 && seats[SIZE-1] != 0) //Check if at least a seat is available
{
printf("\n\nSorry the flight is currently full\n\n");
break;
}
reserve_economy(seats, SIZE);
break;
case 3:
/* User chose Display All */
/* economy function call */
display_all(seats, SIZE);
break;
case -99:
return 0;
default:
/* Invalid Choice */
printf("\n\nPlease Select A Valid Choice!!\n\n");
}
}while(1);
}
void reserve_first_class(int s[], int size)
{
int i, seat = 0;
char decision;
/* first class is seats 1-5 (s[0] to s[4]) */
for(i = 0; i < size/2; i++)
{
if(s[i] == 0) /* If seat is empty */
{
s[i] = 1; /* assign the seat to this person */
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break; /* they got a seat, get out of the loop */
}
}
if(seat == 0) /* If they didn't get a seat, ask if they want economy */
{
printf("\nThere are no more available First Class seats\n");
printf("Would you like a seat in Economy? (Y or N) \n");
printf(">>>");
scanf(" %c", &decision); /* Get their choice */
if(decision == 'y' || decision == 'Y') /* they chose yes */
reserve_economy(s, size); /* send them to the economy function */
else
printf("\nNext flight leaves in 3 hours!\n\n");
}
}
void reserve_economy(int s[], int size)
{
int i, seat = 0;
char decision;
/* economy is seats 6-10 (s[5] to s[9]) */
for(i = size/2; i < size; i++)
{
if(s[i] == 0) /* Seat is empty */
{
s[i] = 1; /* assign them the seat */
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break; /* they got a seat, get out of the loop */
}
}
if(seat == 0) /* If they didn't get a seat, ask if they want economy */
{
printf("\nThere are no more available Economy Class seats\n");
printf("Would you like a seat in First Class? (Y or N) \n");
printf(">>>");
scanf(" %c", &decision); /* Get their choice */
if(decision == 'y' || decision == 'Y') /* they chose yes */
reserve_first_class(s, size); /* send them to the economy function */
else
printf("\nNext flight leaves in 3 hours!\n\n");
}
}
void display_all(int seats[], int size)
{
int i;
for(i = 0; i < size/2; i++)
printf("1st_C_seat:%d Stat:%d\n",i, seats[i]);
for(; i < size; i++)
printf("Eco_C_seat:%d Stat:%d\n",i, seats[i]);
}
Little change will be necessary to offer economy if first class is full but nothing about offering first class if economy is full.
At switch() and reserve_economy(int [], int);