Review my finished assignment
I was wondering if any of you C gurus would mind checking out my assignment that's due on Tuesday. It seems to run fine and I think I have it completed but wouldn't mind if some of you guys kind of proof read it and make sure I've done things correctly. Take into consideration we have just been using printf and scanf and basic control structures.
Code:
/* Airline Reservation System */
#include <stdio.h>
#define SIZE 10
void reserve_first_class(int [], int);
void reserve_economy(int [], int);
int main(void)
{
int seats[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int choice;
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Enter -99 to end>>>");
scanf("%d", &choice);
while(choice != -99)
{
if(choice == 1)
{
reserve_first_class(seats, SIZE);
}
else if(choice == 2)
{
printf("You chose choice 2\n\n");
reserve_economy(seats, SIZE);
}
else
printf("\n\nPlease Select A Valid Choice!!\n\n");
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Enter -99 to end>>>");
scanf("%d", &choice);
}
return 0;
}
void reserve_first_class(int s[], int size)
{
int i, seat = 0;
char decision;
for(i = 0; i < size/2; i++)
{
if(s[i] == 0)
{
s[i] = 1;
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break;
}
else
continue;
}
if(seat == 0)
{
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);
if(decision == 'y' || decision == 'Y')
reserve_economy(s, size);
else
printf("\nNext flight leaves in 3 hours!\n\n");
}
}
void reserve_economy(int s[], int size)
{
int i, seat = 0;
for(i = size/2; i < size ; i++)
{
if(s[i] == 0)
{
s[i] = 1;
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break;
}
else
continue;
}
if(seat == 0)
printf("\n\nSorry the flight is currently full\n\n");
}
I would greatly appreciate it if anyone has any insight that could help me.
Re: Review my finished assignment
Quote:
Originally posted by damonbrinkley
I was wondering if any of you C gurus would mind checking out my assignment that's due on Tuesday. It seems to run fine and I think I have it completed but wouldn't mind if some of you guys kind of proof read it and make sure I've done things correctly. Take into consideration we have just been using printf and scanf and basic control structures.
Code:
/* Airline Reservation System */
#include <stdio.h>
#define SIZE 10
void reserve_first_class(int [], int);
void reserve_economy(int [], int);
int main(void)
{
int seats[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int choice;
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Enter -99 to end>>>");
scanf("%d", &choice);
while(choice != -99)
{
if(choice == 1)
{
reserve_first_class(seats, SIZE);
}
else if(choice == 2)
{
printf("You chose choice 2\n\n");
reserve_economy(seats, SIZE);
}
else
printf("\n\nPlease Select A Valid Choice!!\n\n");
printf("Please type 1 for \"first class\"\n");
printf("Please type 2 for \"economy\"\n");
printf("Enter -99 to end>>>");
scanf("%d", &choice);
}
return 0;
}
void reserve_first_class(int s[], int size)
{
int i, seat = 0;
char decision;
for(i = 0; i < size/2; i++)
{
if(s[ i] == 0)
{
s[ i] = 1;
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break;
}
else
continue;
}
if(seat == 0)
{
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);
if(decision == 'y' || decision == 'Y')
reserve_economy(s, size);
else
printf("\nNext flight leaves in 3 hours!\n\n");
}
}
void reserve_economy(int s[], int size)
{
int i, seat = 0;
for(i = size/2; i < size ; i++)
{
if(s[ i] == 0)
{
s[ i] = 1;
printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
seat = 1;
break;
}
else
continue;
}
if(seat == 0)
printf("\n\nSorry the flight is currently full\n\n");
}
I would greatly appreciate it if anyone has any insight that could help me.
Looks good and straight forward. A couple comments you can take or leave:
1) I would use a do...while to replace the while in red. That way your menu display won't need to be used twice, before the loop and at the end of it.
2) Why use -99 to exit? Why not 0 or 9? They are easier to type.
3) Insead of size/2 to define the number of seats in 1st class, specify a value as a #define. This way you can specify exactly how many seats there are. It took me a few minutes to understand what size/2 meant.
4) Comments, comments, comments. You need to comment your code so the reader doesn't have to figure out what you're trying to accomplish (see 3)