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.