5 Replies - 1891 Views - Last Post: 03 May 2009 - 04:34 PM Rate Topic: -----

#1 bemo55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 23-February 09

Using linked lists to store names

Posted 02 May 2009 - 10:09 PM

Hello! I am trying to take a program i have and store names in linked lists instead of arrays. However i dont really know much about them and i was needing some ideas on how i could accomplish that. Here's the code i have with arrays.

#include <stdio.h>
#include <string.h>

#define SIZE 41
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25
/*function prototypes*/
void askFlight(int,int[],char[][LENGTH],char[][LENGTH]);
void askName(int,char[][LENGTH],char[][LENGTH]);
void printBPass(char[][LENGTH],char[][LENGTH],int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void chkEClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void prFlight(char[][LENGTH],char[][LENGTH],int,int[]);


int main(void)
{
	int seatnumber=0;/*passenger seat number*/
	int seat[SIZE]={0};/*array to verify seats availible*/
	char choice2;
	char firstname[SIZE][LENGTH]={" "};/*array to store user names*/
	char lastname[SIZE][LENGTH]={" "};/*array to store user names*/
	int choice=0;

	do{
		

	askFlight(choice,seat,firstname,lastname);/*prompt user to choose flight*/
	
	
	






	puts("\nWould you like to board another flight?");/*prompt user to choose to board flight*/
	scanf("%*c%c", &choice2);/*read in choice*/



	}while (choice2 == 'y' || choice2 == 'Y');

	if(choice2=='n' || choice2=='N')/*if choice = no print plane roster*/
		prFlight(firstname,lastname,seatnumber,seat);



  
  return 0;/*end main*/


	

}
void askFlight(int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{/* begin askFlight*/
	 
	
	int seatnumber=0;/*holds customer seatnumber*/
	char economyChoice=0;//holds char "choice"
	char fclassChoice=0;//holds char "choice"
	
  

  printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
  printf("Please type 1 for first class\nplease type 2 for economy.");//prompt user to choose between firstclass and economy
  scanf("%d", &choice);//read in choice

  chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);//check if seats are availible in firstclass
  chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);//check if seats are availible in firstclass

 

//end askFlight function
}

void askName(int seatnumber,char firstname[][LENGTH], char lastname[][LENGTH])
{// begin askName
	//int i;//counter

	
	

	

	printf("Please enter your first name:\n");//prompt user to enter first name
	scanf("%s", firstname[seatnumber]);// read in firstname into the element of firstname array
									   // corresponding with seatnumber

	

	printf("Please enter your last name:\n");//prompt user to enter lastname
	scanf("%s", lastname[seatnumber]);// read in lastname into the element of lastname array
									  //corresponding with seatnumber

	
	
	

	
	printHeading();//print B-pass heading
	printBPass(firstname,lastname,seatnumber);//print b-pass
	



}//end askName


void printBPass(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber)
{
	printf("\n%s %5s%10d",firstname[seatnumber],lastname[seatnumber],seatnumber);//prints out user b-pass

}
void printHeading(void)
{
	printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	if ( choice==1)//if user chooses firstclass
   {//begin if
	 for( seatnumber=1; seatnumber<=FCLASS; seatnumber++)//loop through seats array to check seat avail.
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1; //seat value in seat element to 1 showing its taken
			   askName(seatnumber,firstname,lastname);// prompt user for name
			   break;//continue asking for flight
		   

		   }//end if
		
		 
	 }//end for
			   
		   
}// end id

		if (seatnumber>FCLASS)//check if firstclass is full
	 {//begin if
		 printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
		 scanf("%*c%c", &economyChoice);//if firstclass is full ask user if they would like economy class

		 switch(economyChoice){
			 case 'y':
			 case 'Y':
				 choice=2;		 //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");//if user does not want firstclaass tell them to board different flight
				 break;

				 }//end switch
		 chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);// check seats in economy

}//end if
   

}//end chkFclass
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	if(choice==2)//if user chooses economy

 {//begin if
	 for( seatnumber=11; seatnumber<SIZE; seatnumber++)//loop thru to check if seats are availible
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1;//seat value in seat element to 1 showing its taken
			   
			   askName(seatnumber,firstname,lastname);//prompt user to enter name
			   break;
				 
								  
			   } //end if
		}//end for
  
   
}//end if
  if (seatnumber>SIZE)//check if economy is full
	 {//begin if
		 printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
		 scanf("%*c%c", &fclassChoice);//if economy is full ask user if they would like first class

		 switch(fclassChoice){//begin switch
			 case 'y':
			 case 'Y':
				 choice=1;			  //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;//if user does not want first class, tell them to board another flight
		 }//end switch
		 chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);//check seats in first class
  }//end if
		 		 
}//end chkEclass

void prFlight(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber,int seat[])
{
	int cnt=0;//counter
	int cnt2=0;//counter
	int i;//counter to index array
	int j;//counter to index array
	

	

	printf("\n********FIRST CLASS**********\n");//custom heading

	printHeading();//print heading
	
	

	

	for(i=1;i<=FCLASS;i++){//loop thru contents of array

		printf("\n%s %5s%10d",firstname[i],lastname[i],i);//print out names and seatnumbers
		if (seat[i]==1)//if a seat is taken increase count of people on classs
			cnt++;
	}
	
	printf("\nNumber of people in first class is %d:",cnt);//print out number of people in firstclasss
		
	printf("\n********FIRST CLASS**********");
	printf("\n********ECONOMY**********\n");
	printHeading();//print heading
	for(j=11;j<SIZE;j++){//loop thru arrays to access info
		if (seat[j]==1)//if a seat is taken increase count of people on economy
			cnt2++;
		printf("\n%s %5s%10d",firstname[j],lastname[j],j);//display roster for economy
	}
	

	printf("\nNumber of people in economy class is %d:",cnt2);//print out number of people in economy
	printf("\n********ECONOMY**********");

	printf("\nTotal number of people on plane is %d\n", cnt+cnt2);//print out total number of people
		
		

	
	
	

}



Any suggestions would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Using linked lists to store names

#2 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Using linked lists to store names

Posted 03 May 2009 - 01:56 AM

Have you worked with linked lists before? Finding a quick tutorial on them might help.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7378
  • View blog
  • Posts: 15,309
  • Joined: 16-October 07

Re: Using linked lists to store names

Posted 03 May 2009 - 07:03 AM

To be honest, I'd fix the list version first. Paralle arrays are always a bad choice. The amount of juggling you have to do to support them is tragic. You can do the same thing with a struct and a list.

typedef struct BPassStruct {
	int classType;
	int seat;
	char firstname[LENGTH];
	char lastname[LENGTH];
} BPass;

typedef struct BPassListStruct {
	BPass items[SIZE];
	int currentSize;
	int maxSize;
} BPassList;



If you do this correctly, the linked list version is basically just this:
typedef struct NodeStruct {
	BPass data;
	struct NodeStruct *next;
} Node;

typedef struct BPassListStruct {
	Node *head;
} BPassList;



The function declaration don't change. The guts of some of those functions change, but it's not as drastic as what you're looking at. In any case, you can still implement the linked list version with those three structs.
Was This Post Helpful? 0
  • +
  • -

#4 bemo55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 23-February 09

Re: Using linked lists to store names

Posted 03 May 2009 - 03:24 PM

Thanks for the help. Now im just stuck on how i would use the malloc() function in my functions to store and print data. Any suggestions on that?

This post has been edited by bemo55: 03 May 2009 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 bemo55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 23-February 09

Re: Using linked lists to store names

Posted 03 May 2009 - 04:19 PM

Okay so i have attempted to get this working with my linked list, but have run into some problems.
Everything goes fine until it has to print the names. i get this output:

Welcome to the Airline Reservations System

Would you like to fly First class or economy?
Please type 1 for first class
please type 2 for economy.1
Please enter your first name:
brandon
Please enter your last name:
Customer Name Seat Number
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
═════════════════════════════════════════════════════════════════brandon brandon
1
Would you like to board another flight?
y
Welcome to the Airline Reservations System

Would you like to fly First class or economy?
Please type 1 for first class
please type 2 for economy.2
Please enter your first name:
brandon
Please enter your last name:
Customer Name Seat Number
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
════════════════════════════════════════════════════════════════════════════════
═════════════════════════════════════════════════════════════════brandon brandon
11
Would you like to board another flight?

. I dont unserstand 1.)why it is taking in what i type as firstname as lastname too 2.) why it is printing garbage.

here is my code.
#include <stdio.h>
#include <string.h>

#define SIZE 41
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25

typedef struct BPassStruct {
		int classType;
		int seat;
		char firstname[SIZE][LENGTH];
		char lastname[SIZE][LENGTH];
} bPass;

typedef struct NodeStruct {
		bPass data;
		struct NodeStruct *next;
} node;
typedef struct BPassListStruct {
		node *head;
} bPassList;


/*function prototypes*/
void askFlight(int,int[],node *);
void askName(int,char[][LENGTH],char[][LENGTH]);
void printBPass(/*char[][LENGTH],char[][LENGTH]*/node *,int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],node */*,char[][LENGTH],char[][LENGTH]*/);
void chkEClass(int,int,int,int,int[],node */*,char[][LENGTH],char[][LENGTH]*/);
void prFlight(/*char[][LENGTH],char[][LENGTH]*/,int,int[]);
void sortName(void);

int main(void)
{
	int seatnumber=0;/*passenger seat number*/
	int seat[SIZE]={0};/*array to verify seats availible*/
	char choice2;
	//char firstname[SIZE][LENGTH]={" "};/*array to store user names*/
	//char lastname[SIZE][LENGTH]={" "};/*array to store user names*/
	int choice=0;
	node *head=NULL;

	do{
		node *temp;
		temp = (node*)malloc(sizeof(node));

		

	askFlight(choice,seat,temp);/*prompt user to choose flight*/
	
	
	






	puts("\nWould you like to board another flight?");/*prompt user to choose to board flight*/
	scanf("%*c%c", &choice2);/*read in choice*/



	}while (choice2 == 'y' || choice2 == 'Y');

	if(choice2=='n' || choice2=='N')/*if choice = no print plane roster*/
		prFlight(/*firstname,lastname,*/seatnumber,seat);



  
  return 0;/*end main*/


	

}
void askFlight(int choice,int seat[],node *temp)
{/* begin askFlight*/
	 
	
	int seatnumber=0;/*holds customer seatnumber*/
	char economyChoice=0;//holds char "choice"
	char fclassChoice=0;//holds char "choice"
	
  

  printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
  printf("Please type 1 for first class\nplease type 2 for economy.");//prompt user to choose between firstclass and economy
  scanf("%d", &choice);//read in choice

  chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp/*,firstname,lastname*/);//check if seats are availible in firstclass
  chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp/*,firstname,lastname*/);//check if seats are availible in firstclass

 

//end askFlight function
}

void askName(int seatnumber,node *temp)
{// begin askName
	//int i;//counter

	
	

	

	printf("Please enter your first name:\n");//prompt user to enter first name
	scanf("%i", temp->data.firstname);// read in firstname into the element of firstname array
									   // corresponding with seatnumber

	

	printf("Please enter your last name:\n");//prompt user to enter lastname
	scanf("%s", temp->data.lastname);// read in lastname into the element of lastname array
									  //corresponding with seatnumber

	
	
	

	
	printHeading();//print B-pass heading
	printBPass(/*firstname,lastname,*/temp,seatnumber);//print b-pass
	



}//end askName


void printBPass(node *temp, int seatnumber)
{
	printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,seatnumber);//prints out user b-pass

}
void printHeading(void)
{
	printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],node *temp)
{
	if ( choice==1)//if user chooses firstclass
   {//begin if
	 for( seatnumber=1; seatnumber<=FCLASS; seatnumber++)//loop through seats array to check seat avail.
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1; //seat value in seat element to 1 showing its taken
			   askName(seatnumber,temp);// prompt user for name
			   break;//continue asking for flight
		   

		   }//end if
		
		 
	 }//end for
			   
		   
}// end id

		if (seatnumber>FCLASS)//check if firstclass is full
	 {//begin if
		 printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
		 scanf("%*c%c", &economyChoice);//if firstclass is full ask user if they would like economy class

		 switch(economyChoice){
			 case 'y':
			 case 'Y':
				 choice=2;		 //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");//if user does not want firstclaass tell them to board different flight
				 break;

				 }//end switch
		 chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp);// check seats in economy

}//end if
   

}//end chkFclass
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],node *temp)
{
	if(choice==2)//if user chooses economy

 {//begin if
	 for( seatnumber=11; seatnumber<SIZE; seatnumber++)//loop thru to check if seats are availible
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1;//seat value in seat element to 1 showing its taken
			   
			   askName(seatnumber,temp);//prompt user to enter name
			   break;
				 
								  
			   } //end if
		}//end for
  
   
}//end if
  if (seatnumber>SIZE)//check if economy is full
	 {//begin if
		 printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
		 scanf("%*c%c", &fclassChoice);//if economy is full ask user if they would like first class

		 switch(fclassChoice){//begin switch
			 case 'y':
			 case 'Y':
				 choice=1;			  //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;//if user does not want first class, tell them to board another flight
		 }//end switch
		 chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp);//check seats in first class
  }//end if
		 		 
}//end chkEclass

void prFlight(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber,int seat[])
{
	int cnt=0;//counter
	int cnt2=0;//counter
	int i;//counter to index array
	int j;//counter to index array
	

	

	printf("\n********FIRST CLASS**********\n");//custom heading

	printHeading();//print heading
	
	

	

	for(i=1;i<=FCLASS;i++){//loop thru contents of array

		printf("\n%s %5s%10d",firstname[i],lastname[i],i);//print out names and seatnumbers
		if (seat[i]==1)//if a seat is taken increase count of people on classs
			cnt++;
	}
	
	printf("\nNumber of people in first class is %d:",cnt);//print out number of people in firstclasss
		
	printf("\n********FIRST CLASS**********");
	printf("\n********ECONOMY**********\n");
	printHeading();//print heading
	for(j=11;j<SIZE;j++){//loop thru arrays to access info
		if (seat[j]==1)//if a seat is taken increase count of people on economy
			cnt2++;
		printf("\n%s %5s%10d",firstname[j],lastname[j],j);//display roster for economy
	}
	

	printf("\nNumber of people in economy class is %d:",cnt2);//print out number of people in economy
	printf("\n********ECONOMY**********");

	printf("\nTotal number of people on plane is %d\n", cnt+cnt2);//print out total number of people
		
		

	
	
	

}



Any suggestions?


p.s. ignore the things i have commented out. :)
Was This Post Helpful? 0
  • +
  • -

#6 bemo55   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 23-February 09

Re: Using linked lists to store names

Posted 03 May 2009 - 04:34 PM

Okay guys. This stuff isnt as hard as i was thinking i already got the first problem figured out. Now imj getting this output.
Welcome to the Airline Reservations System

Would you like to fly First class or economy?
Please type 1 for first class
please type 2 for economy.1
Please enter your first name:
Brandon
Please enter your last name:
Meaux
Customer Name Seat Number
Brandon Meaux 1
Would you like to board another flight?
y
Welcome to the Airline Reservations System

Would you like to fly First class or economy?
Please type 1 for first class
please type 2 for economy.2
Please enter your first name:
Bill
Please enter your last name:
Murray
Customer Name Seat Number
Bill Murray 11
Would you like to board another flight?
y
Welcome to the Airline Reservations System

Would you like to fly First class or economy?
Please type 1 for first class
please type 2 for economy.1
Please enter your first name:
Jim
Please enter your last name:
Carrey
Customer Name Seat Number
Jim Carrey 2
Would you like to board another flight?
n

********FIRST CLASS**********
Customer Name Seat Number
Number of people in first class is 0:
********FIRST CLASS**********
********ECONOMY**********
Customer Name Seat Number
Number of people in economy class is 0:
********ECONOMY**********
Total number of people on plane is 0
Press any key to continue . . .

as you can see when i tell it i do not want to board any more flights it isnt print out the plane roster.Here is my code so far.

#include <stdio.h>
#include <string.h>

#define SIZE 41
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25

typedef struct BPassStruct {
		int classType;
		int seat;
		char firstname[SIZE][LENGTH];
		char lastname[SIZE][LENGTH];
} bPass;

typedef struct NodeStruct {
		bPass data;
		struct NodeStruct *next;
} node;
typedef struct BPassListStruct {
		node *head;
} bPassList;


/*function prototypes*/
void askFlight(int,int[],node *);
void askName(int,char[][LENGTH],char[][LENGTH]);
void printBPass(/*char[][LENGTH],char[][LENGTH]*/node *,int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],node */*,char[][LENGTH],char[][LENGTH]*/);
void chkEClass(int,int,int,int,int[],node */*,char[][LENGTH],char[][LENGTH]*/);
void prFlight(/*char[][LENGTH],char[][LENGTH]*/,int,int[]);
void sortName(void);

int main(void)
{
	int seatnumber=0;/*passenger seat number*/
	int seat[SIZE]={0};/*array to verify seats availible*/
	char choice2;
	//char firstname[SIZE][LENGTH]={" "};/*array to store user names*/
	//char lastname[SIZE][LENGTH]={" "};/*array to store user names*/
	int choice=0;
	node *head=NULL;

	do{
		node *temp;
		temp = (node*)malloc(sizeof(node));

		

	askFlight(choice,seat,temp);/*prompt user to choose flight*/
	
	
	






	puts("\nWould you like to board another flight?");/*prompt user to choose to board flight*/
	scanf("%*c%c", &choice2);/*read in choice*/



	}while (choice2 == 'y' || choice2 == 'Y');

	if(choice2=='n' || choice2=='N')/*if choice = no print plane roster*/
		prFlight(/*firstname,lastname,*/seatnumber,seat);



  
  return 0;/*end main*/


	

}
void askFlight(int choice,int seat[],node *temp)
{/* begin askFlight*/
	 
	
	int seatnumber=0;/*holds customer seatnumber*/
	char economyChoice=0;//holds char "choice"
	char fclassChoice=0;//holds char "choice"
	
  

  printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
  printf("Please type 1 for first class\nplease type 2 for economy.");//prompt user to choose between firstclass and economy
  scanf("%d", &choice);//read in choice

  chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp/*,firstname,lastname*/);//check if seats are availible in firstclass
  chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp/*,firstname,lastname*/);//check if seats are availible in firstclass

 

//end askFlight function
}

void askName(int seatnumber,node *temp)
{// begin askName
	//int i;//counter

	
	

	

	printf("Please enter your first name:\n");//prompt user to enter first name
	scanf("%s", temp->data.firstname);// read in firstname into the element of firstname array
									   // corresponding with seatnumber

	

	printf("Please enter your last name:\n");//prompt user to enter lastname
	scanf("%s", temp->data.lastname);// read in lastname into the element of lastname array
									  //corresponding with seatnumber

	
	
	

	
	printHeading();//print B-pass heading
	printBPass(/*firstname,lastname,*/temp,seatnumber);//print b-pass
	



}//end askName


void printBPass(node *temp, int seatnumber)
{
	printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,seatnumber);//prints out user b-pass

}
void printHeading(void)
{
	printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],node *temp)
{
	if ( choice==1)//if user chooses firstclass
   {//begin if
	 for( seatnumber=1; seatnumber<=FCLASS; seatnumber++)//loop through seats array to check seat avail.
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1; //seat value in seat element to 1 showing its taken
			   askName(seatnumber,temp);// prompt user for name
			   break;//continue asking for flight
		   

		   }//end if
		
		 
	 }//end for
			   
		   
}// end id

		if (seatnumber>FCLASS)//check if firstclass is full
	 {//begin if
		 printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
		 scanf("%*c%c", &economyChoice);//if firstclass is full ask user if they would like economy class

		 switch(economyChoice){
			 case 'y':
			 case 'Y':
				 choice=2;		 //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");//if user does not want firstclaass tell them to board different flight
				 break;

				 }//end switch
		 chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp);// check seats in economy

}//end if
   

}//end chkFclass
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],node *temp)
{
	if(choice==2)//if user chooses economy

 {//begin if
	 for( seatnumber=11; seatnumber<SIZE; seatnumber++)//loop thru to check if seats are availible
		 {//begin for
		   if(seat[seatnumber]==0)
			  {//begin if
			   seat[seatnumber]=1;//seat value in seat element to 1 showing its taken
			   
			   askName(seatnumber,temp);//prompt user to enter name
			   break;
				 
								  
			   } //end if
		}//end for
  
   
}//end if
  if (seatnumber>SIZE)//check if economy is full
	 {//begin if
		 printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
		 scanf("%*c%c", &fclassChoice);//if economy is full ask user if they would like first class

		 switch(fclassChoice){//begin switch
			 case 'y':
			 case 'Y':
				 choice=1;			  //read in choice
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;//if user does not want first class, tell them to board another flight
		 }//end switch
		 chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,temp);//check seats in first class
  }//end if
		 		 
}//end chkEclass

void prFlight(node *temp, int seatnumber,int seat[])
{
	int cnt=0;//counter
	int cnt2=0;//counter
	int i;//counter to index array
	int j;//counter to index array
	

	

	printf("\n********FIRST CLASS**********\n");//custom heading

	printHeading();//print headingy

	
	

	

	while(temp != NULL){//loop thru contents of array
		i++;

		printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,i);//print out names and seatnumbers
		if (seat[i]==1)//if a seat is taken increase count of people on classs
			cnt++;
	}
	
	printf("\nNumber of people in first class is %d:",cnt);//print out number of people in firstclasss
		
	printf("\n********FIRST CLASS**********");
	printf("\n********ECONOMY**********\n");
	printHeading();//print heading
	while(temp!=NULL){//loop thru arrays to access info
		j++;
		if (seat[j]==1)//if a seat is taken increase count of people on economy
			cnt2++;
		printf("\n%s %5s%10d",temp->data.firstname,temp->data.lastname,j);//display roster for economy
	}
	

	printf("\nNumber of people in economy class is %d:",cnt2);//print out number of people in economy
	printf("\n********ECONOMY**********");

	printf("\nTotal number of people on plane is %d\n", cnt+cnt2);//print out total number of people
		
		

	
	
	

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1