C Programming Please fix the code so that it can have the output like the picture Problem: When I, for example, choose t

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

C Programming Please fix the code so that it can have the output like the picture Problem: When I, for example, choose t

Post by answerhappygod »

C Programming
Please fix the code so that it can have the output like the picture
Problem: When I, for example, choose to reserve the seat 1A, the code just ends. Please help fix the code to ask for passenger ID and 'seat xx is now occupied by passenger xx" like the picture.
Output:
C Programming Please Fix The Code So That It Can Have The Output Like The Picture Problem When I For Example Choose T 1
C Programming Please Fix The Code So That It Can Have The Output Like The Picture Problem When I For Example Choose T 1 (19.04 KiB) Viewed 11 times
Code:
#include <stdio.h>#include <ctype.h>
#define NUM_ROWS 12#define NUM_LETTERS 4#define NUM_SEATS (NUM_ROWS * NUM_LETTERS)
const int INVALID_SEAT = -1;int a[12][4];
int g_reservations[NUM_SEATS];
void ClearReservations(void) {}
void PrintSeatName(int seat){ printf("XX");}
int IsValidSeatIndex(int seat){ seat; return 0;}
int GetSeatInput(void){ return INVALID_SEAT;}
int GetSeatFromUser(const int isOccupied){ int inputSeat = GetSeatInput();
if (IsValidSeatIndex(inputSeat)) { if (isOccupied != 0 && g_reservations[inputSeat] == 0) { printf("ERROR: Sorry, seat "); PrintSeatName(inputSeat); printf(" was expected to be occupied but is empty.\n"); inputSeat = INVALID_SEAT; } else if (isOccupied == 0 && g_reservations[inputSeat] != 0) { printf("ERROR: Sorry, seat "); PrintSeatName(inputSeat); printf(" was expected to be empty but is occupied by passenger %d.\n", g_reservations[inputSeat]); inputSeat = INVALID_SEAT; } } else { printf("ERROR: Sorry, that seat is not valid.\n"); return INVALID_SEAT; }
return inputSeat;}
int GetOccupiedSeatFromUser(void){ return GetSeatFromUser(/* isOccupied */ 1);}
int GetEmptySeatFromUser(void){ return GetSeatFromUser(/* isOccupied */ 0);}
int GetPassengerIdFromUser(void){ int passid = 0; printf("Enter passenger ID: "); scanf_s("%d", &passid); return passid;}
int FindSeatWithPassenger(const int passengerId){ passengerId;
return INVALID_SEAT;}
void ReserveSeat(void){ char s[10]; printf("Enter a seat to reserve: "); scanf_s("%s", &s); int id = GetPassengerIdFromUser(); if (id >= 1 && id <= 999) { a[s[0] - '0'][s[1] - 'A'] = id; int row = s[0] - '0'; int col = s[1] - 'A'; int seatIndex = (row - 1) * 4 + col; g_reservations[seatIndex] = id; printf("Seat %s is now occupied by passenger %d", s, id); }}
void CancelReservation(void){ printf("Enter a seat to cancel: "); char s[10]; scanf_s("%s", s); if (GetOccupiedSeatFromUser()) {
}}
void MoveSeat(void){ int fromSeat; printf("FROM which seat to move: "); fromSeat = GetOccupiedSeatFromUser();
if (IsValidSeatIndex(fromSeat)) { int toSeat; printf("TO which seat to move: "); toSeat = GetEmptySeatFromUser();
if (IsValidSeatIndex(toSeat)) { g_reservations[toSeat] = g_reservations[fromSeat]; g_reservations[fromSeat] = 0;
printf("Moved passenger %d from seat ", g_reservations[toSeat]); PrintSeatName(fromSeat); printf(" to seat "); PrintSeatName(toSeat); printf(".\n"); } }}
void DisplaySeating(void){ int row, letter;
printf("\n------------------------------"); printf("\n Current Seating Reservations"); printf("\n------------------------------");
printf("\n | A | B | C | D |"); printf("\n-----+-----+-----+-----+-----+"); printf("\n");
for (row = 0; row < NUM_ROWS; ++row) { const int rowNumber = row + 1; printf("%4d |", rowNumber); for (letter = 0; letter < NUM_LETTERS; ++letter) { int seat = row * NUM_LETTERS + letter; int id = g_reservations[seat];
if (id == 0) printf(" |"); else printf(" %d |", id); } printf("\n"); }}
void DisplayMenu(void){ printf("\n"); printf("---------------------------\n"); printf("--- CheapSkate Air Menu ---\n"); printf("---------------------------\n"); printf(" (0) Exit the Program\n"); printf(" (1) Reserve a seat\n"); printf(" (2) Cancel Reservation\n"); printf(" (3) Move Seat Reservation\n"); printf(" (4) Display Current Seating\n");}
int GetMenuChoice(void){ for (;;) { int input; DisplayMenu(); printf("\nPlease choose: "); if (scanf_s("%d", &input) < 1) { printf("ERROR: Invalid input, exiting program.\n"); return 0; } if (input >= 0 && input <= 4) return input; printf("ERROR: %d is invalid. Please choose again.\n", input); }}
int main(void){ int choice = 0; ClearReservations(); do { choice = GetMenuChoice();
if (choice == 1) { ReserveSeat(); } else if (choice == 2) { CancelReservation(); } else if (choice == 3) { MoveSeat(); } else if (choice == 4) { DisplaySeating(); } } while (choice != 0);
return 0;}
CheapSkate Air Menu (0) Exit the Program (1) Reserve a seat (2) Cancel Reservation (3) Move Seat Reservation (4) Display Current Seating Please choose: 1 Enter a seat to reserve: 2B Enter passenger ID: 30 Seat 2B is now occupied by passenger 30. CheapSkate Air Menu (0) Exit the Program (1) Reserve a seat (2) Cancel Reservation (3) Move Seat Reservation (4) Display Current Seating Please choose: 4 Current Seating Reservations | А B | C | D | | 1 | 2 | 3 4 | 5 | 6 | 7 | 8 | co 9| 10 | 11 | 12 | | 30 | | | |
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply