Stuck with function

This is a discussion on Stuck with function within the C Programming forums, part of the General Programming Boards category; I have a function in my program called BoardingPass which is suppose to print the seat number selected in function ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Question Stuck with function

    I have a function in my program called BoardingPass which is suppose to print the seat number selected in function AvailableSeats, but I cannot get the right seat number to print when the BoardingPass function is called, please point me in the right direction
    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" );
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Correction function is called SeatsAvailable, not AvailableSeats

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    &a should just be the variable name of 'a'

    Also use %d and not %i.

    Why is "airplane[seat] = a" commented out? What was your thought process with that?
    Last edited by slingerland3g; 11-05-2009 at 02:57 PM. Reason: typo

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Smile

    That fixed the issue, and thanks a bunch for your help.

    The commented line you mention was me trying to fix the problem, my thought process was definitely going down the wrong path, and I forgot to remove the line of code.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slingerland3g View Post
    Also use %d and not %i.
    Um, Err? Why would you say such a thing? %i is quite useful! What is your justification? <not antagonistic, but asking for information's sake> There are two camps that I have found: Those that were taught %i and those that were taught %d. I have found that %i is a bit more forgiving with the integral input than %d (%i allows the input 0x44fdd, however, %d sees this as the value 0).

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by Kennedy View Post
    Um, Err? Why would you say such a thing? %i is quite useful! What is your justification? <not antagonistic, but asking for information's sake> There are two camps that I have found: Those that were taught %i and those that were taught %d. I have found that %i is a bit more forgiving with the integral input than %d (%i allows the input 0x44fdd, however, %d sees this as the value 0).

    I was out of step, this is just my preference and there is functionally no difference in using either %d or %i. It would be nice to know the history of why there are two specifiers, I just happen to use and see %d being the dominant specifier in most cases.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by slingerland3g
    I was out of step, this is just my preference and there is functionally no difference in using either %d or %i. It would be nice to know the history of why there are two specifiers, I just happen to use and see %d being the dominant specifier in most cases.
    I happened to answer this elsewhere recently, but Kennedy effectively gave the same answer: functionally there is a difference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by laserlight View Post
    I happened to answer this elsewhere recently, but Kennedy effectively gave the same answer: functionally there is a difference.
    True, but in this case %d is preferred. Using %i can be dangerous as this will be platform/compiler dependent then if I understand your thinking. From a quick google search on the differences I really could not find any concrete definition. But what you are saying I will make a mental note of this. Simply stating, I have always used %d with no issue....so far!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by slingerland3g
    Using %i can be dangerous as this will be platform/compiler dependent then if I understand your thinking.
    No, it will not, since the syntax rules for integer constants in C is well defined by the C standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by laserlight View Post
    No, it will not, since the syntax rules for integer constants in C is well defined by the C standard.

    Thanks found the source, in the standards, that address the uses of %i, for hex and octal. Thanks.

    If you are not sure on the input type either decimal, hex or octal then go with %i, if you are sure of the input type then go with %d, is my logic on this.
    Last edited by slingerland3g; 11-10-2009 at 10:51 AM. Reason: corrected wording

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21