here's the code of the program, the problem is stated in the bottom.
Code:
#include <stdio.h>
#define SIZE 10
int SeatDis(int[], int);
main() {
static int Ar[SIZE] = { 0 };
int a, ok;
puts("Enter 1 for "First class"");
puts("Enter 2 for "Econom class"");
puts("Enter 3 to Quit");
scanf_s("%d", &a);
if (a == 1 || a == 2) {
ok = SeatDis(Ar, (a - 1) * 5);
if (ok != 0){
printf("Your seat num is %d\n", ok);
main();
}
if (a == 1 && SeatDis(Ar, 5)) {
printf("No seats found in First Class\n");
printf("Wish to buy a seat in Econom class?(1 or Other = Yes : 0 = No)\n");
scanf_s("%d", &a);
if (a) {
printf("Your seat num is %d\n", SeatDis(Ar, 5));
main();
}
}
else {
if (a == 2 && SeatDis(Ar, 0)) {
printf("No seats found in Econom Class\n");
printf("Wish to buy a seat in First class?(1 = Yes : 2 = No)\n");
scanf_s("%d", &a);
if (a) {
printf("Your seat num is %d\n", SeatDis(Ar, 0));
main();
}
}
else printf("Next flight in three hours time from now...\n");
}
}
else {
if (a != 3) {
printf("Something went wrong...\n");
main();
}
}
system("pause");
return 0;
}
int SeatDis(int Ar[], int c) {
for (int b = c + 5; c < b; c++) {
if (Ar[c] == 0) {
Ar[c] = 1;
return c + 1;
}
}
return 0;
}
The program asks you If you would like to buy a seat in the plane. Depending on your decision and amount of spare seats it reacts..
In the plane first (1-5) seats are First class, 6-10 are Econom.
The program works fine except of one bug that I can't figure out how to solve.
When all of the seats in one class are taken It asks me if I want to change my class - I answer: 1 meaning ok, but instead of giving me the seat from start (1-5 or 6-10) It gives me 2-10 or 7-10 seats, meaning that one seat had been lost, because no one had bought it.