Thread: Need help with a switch statement

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Need help with a switch statement

    I'm writing a program that lets a user look at and alter the manifest of a small plane. My functions are working properly but when I go through the switch statement it runs through it one more time than I need it to.

    Here is my code:
    Code:
    #include "stdafx.h"
    #include "string.h"
    
    #define maxSeats 12
    #define nameLength 25
     
    char menuDisplay (void);
    int emptySeats (void);
    void listEmpty (void);
    void assignSeat (void);
    void deleteSeat (void);
    
    struct seatInformation
    {	unsigned int seatID;
    	unsigned int seatMarker;
    	char lastName[nameLength];
    	char firstName[nameLength];
    }	seats[maxSeats];
    
    int _tmain(int argc, _TCHAR* argv[])
    {	int i;
    	for(i=0;i<maxSeats;i++)	
    		seats[i].seatID = (i+1);
    	for(i=0;i<maxSeats;i++)
    		seats[i].seatMarker = 0;
    	char decision='a';
    	
    	while (decision!= 'e')
    	{	decision = menuDisplay();
    		switch(decision)
    		{	case 'A' :
    			case 'a' : printf("There are %d empty seats on this flight\n\n",emptySeats());
    					break;
    			case 'B' :
    			case 'b' : listEmpty();
    					break;
    			case 'C' :
    			case 'c' : assignSeat();
    					break;
    			case 'D' :
    			case 'd' : deleteSeat();
    					break;
    			case 'E' :
    			case 'e' : break;
    			default : printf("Invalid Entry\n");
    				break;
    		}
    		
    		printf("\n");
    	}
    	
    	return 0;
    }
    
    char menuDisplay (void)
    {	char c;
    	printf("Choose a function :\n");
    	printf("   a)Show number of empty seats\n");
    	printf("   b)Show list of empty seats\n");
    	printf("   c)Assign a seat\n");
    	printf("   d)Delete a seat assignment\n");
    	printf("   e)Quit\n");
    	scanf("%c",&c);
    	return c;
    }
    
    int emptySeats (void)
    {	int i;
    	int count = 0;
    	for(i=0;i<maxSeats;i++)
    	{	if(seats[i].seatMarker == 0)
    			count++;
    	}
    	return count;
    }
    
    void listEmpty (void)
    {	int i;
    	for(i=0;i<maxSeats;i++)
    	{	if(seats[i].seatMarker == 0)
    			printf("Seat number %d is unassigned\n",i+1);		
    	}
    	printf("\n");
    }
    
    void assignSeat (void)
    {	int seat;
    	printf("Select a seat to assign: ");
    	scanf("%d", &seat);
    	if(seats[seat-1].seatMarker != 1)
    	{	printf("\nFill in information below to assign a passenger to seat number %d \n", seat);
    		printf("Passenger first name: ");
    		scanf("%s", &seats[seat-1].firstName);
    		printf("Passenger last name: ");
    		scanf("%s", &seats[seat-1].lastName);
    		seats[seat-1].seatMarker = 1;
    		printf("\nPassenger Information:");
    		printf("\nSeat Number: %d\n", seat);
    		printf("First Name: %s\n", seats[seat-1].firstName);
    		printf("Last Name: %s\n", seats[seat-1].lastName);
    		printf("SEAT CONFIRMED\n\n");
    	}
    	else
    		printf("Seat %d is already taken", seat);
    }
    
    void deleteSeat (void)
    {	int seat;
    	char x[] = "";
    	printf("Select a seat to delete: ");
    	scanf("%d", &seat);
    	if(seats[seat-1].seatMarker == 1)
    	{	seats[seat-1].seatMarker = 0;
    		strcpy(seats[seat-1].firstName, x);
    		strcpy(seats[seat-1].lastName, x);
    		printf("Seat %d has been deleted\n",seat);
    	}
    	else
    		printf("Seat %d is not assigned\n",seat);
    }
    And here is what happens when I run:
    -----------------------------------------------------------------------
    Choose a function :
    a)Show number of empty seats
    b)Show list of empty seats
    c)Assign a seat
    d)Delete a seat assignment
    e)Quit
    a
    There are 12 empty seats on this flight

    Choose a function :
    a)Show number of empty seats
    b)Show list of empty seats
    c)Assign a seat
    d)Delete a seat assignment
    e)Quit
    Invalid Entry

    Choose a function :
    a)Show number of empty seats
    b)Show list of empty seats
    c)Assign a seat
    d)Delete a seat assignment
    e)Quit

    -------------------------------------------------------------------------

    It seems as if after it runs through the switch the first time my variable 'decision'
    is getting assigned a different variable because it triggers the default switch
    statement. I'm not sure what is going on so any help would be appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because a <enter> is two characters and scanf() is leaving the Enter key (newline) in the input buffer after you read out a single character.

    Try it like this...
    Code:
    scanf(" %c",&c);
    Note the space inserted between the " and % .... That tells scanf() to ignore invisible characters.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Thanks for the help it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using a switch statement
    By dukebdx12 in forum C++ Programming
    Replies: 11
    Last Post: 03-26-2008, 01:58 PM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. Using Switch statement
    By Ubha in forum C Programming
    Replies: 8
    Last Post: 12-24-2006, 12:34 AM
  5. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM