plz fix the code below USING VISUAL STUDIO ONLY - get rid of global a array - put the logic for '0' and 'A' to GetSeatI
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
plz fix the code below USING VISUAL STUDIO ONLY - get rid of global a array - put the logic for '0' and 'A' to GetSeatI
plz fix the code below USING VISUAL STUDIO ONLY- get rid of global a array - put the logic for '0' and 'A' to GetSeatInputthe code does not process after 'enter a seat to reserve' plz fix thisUSE VISUAL CODE ONLY NO ONLINE COMPILER #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){ //if seat number is less than 0 or more than 12 display error printf("SEAT - "); printf(seat); if(seat<=0 || seat>=NUM_ROWS){ printf("Entered Seat index is invalid"); return 0; } return 1;}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("%d", &passid); //loop until the entered passenger ID is within range while(passid<=1 || passid>=1000){ printf("Entered passenger ID is incorrect please enter again-\n"); scanf("%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); //create a pointer to extract and store the row number from seat number char* sPtr = s; // col character variable to store the entered column int size = strlen(sPtr); char col; if (size == 2) { col = sPtr[1]; } else { col = sPtr[2]; } printf("\n%c\n", col); // loop and keep taking input until correct seat is entered while (col != 'A' && col != 'B' && col != 'C' && col != 'D') { printf("Invalid Seat, please enter again\n"); scanf("%s", &s); sPtr = s; size = strlen(sPtr); if (size == 2) { col = sPtr[1]; } else { col = sPtr[2]; } } 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); 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; if(rowNumber > 9) { printf("%d |", rowNumber); } else { printf(" %d |", 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("%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;}