Please help with reconfiguring my C program, I need to make a code for deals wit
ID: 3846793 • Letter: P
Question
Please help with reconfiguring my C program, I need to make a code for deals with reservsation for a single night hotel with only 3 rooms numbered 1,2,3. MY CODE IS ATTACHED BELOW AT THE END.
RECONFIGURE TO ADD:
-linked lists to implement the reservation list and the waiting list.
-ONLY HAVE 3 ROOMS NUMBERED ROOMS 1,2,3,
-use a linked list to represent the reservation list, reservation list can have at most as many nodes as there are rooms .
-(r, c, w, or l) must be implemented using programmer-defined functions, one per command
-waiting list must be implemented as a queue (insert nodes at the back of the list and remove nodes from the front of the list when a room becomes available
-list reservations, entries on the waiting list you should also print the reservation number and name of all elements on the waiting list
#include <stdio.h>
#include "stdafx.h"
#include <string.h> //STRING LIBRARY
#include <stdlib.h>
//DECLARE USER-DEFINED FUNCTIONS
int isSomeRoomFree(int *rooms, int n);
int isResvIdPresent(int *rooms, int n, int resID);
void cancelReservation(int *rooms, char roomReserve, int *waiting, char waitingName, int noOfRooms, int *waitingCount, char *reservationName);
void removeWaiting(int *waiting, char waitingName[][15], int n, int indexToRemove);
void printInfo(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int waitingCount);
int isSomeRoomFree(int *rooms, int n);
void reserveRoomIfPossible(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int *reservationId, int *waitingCount, char* reservationName, int *waitingId);
//THESE VARIABLES CAN ONLY BE READ
#define MAXWAITING 3
#define NO_OF_ROOMS 3
#define maxName 15
// checks if some room is free
int isSomeRoomFree(int *rooms, int n) {
int i = 0;
for (; i<n; i++) {
if (rooms[i] == -1) {
return i;
}
}
return -1;
}
// checks if some reservation Id is valid or not
int isResvIdPresent(int *rooms, int n, int resID) {
int i = 0;
for (; i<n; i++) {
if (rooms[i] == resID) {
return i;
}
}
return -1;
}
// checks if someone is waiting for room
int isAnyoneWaiting(int *waiting, int n) {
int i = 0;
for (; i<n; i++) {
if (waiting[i] != -1)
{
return i;
}
}
return -1;
}
// removes waiting list's indexToRemove and shuffles the array
void removeWaiting(int *waiting, char waitingName[][15], int n, int indexToRemove) {
int lastWaitingId = waiting[0];
char * s = waitingName[0];
int i = indexToRemove;
for (; i<n - 1; i++) {
waiting[i] = waiting[i + 1];
memset(&waitingName[i], 0, strlen(waitingName[i]));
strncpy_s(waitingName[i], waitingName[i+1], sizeof(waitingName)); //COPIES NAME OF PERSON 2ND IN LINE TO 1ST IN LINE
}
waiting[i] = -1;
}
// try to reserve a room if possible
void reserveRoomIfPossible(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int *reservationId, int *waitingCount, char* reservationName, int *waitingId) {
int roomNo = isSomeRoomFree(rooms, noOfRooms);
if (roomNo != -1) {
int nameLength = sizeof(reservationName);
rooms[roomNo] = *reservationId;
printf("Room is available. ");
printf("Enter your name for reservation: "); //NAME FOR RESERVATION
scanf_s("%s", reservationName, nameLength); //FOR SOME REASON, HAVE TO PRESS ENTER IN THE COMMAND WINDOW THEN ENTER NAME, THEN HIT ENTER
// empty the string first
memset(roomReserve[roomNo], 0, 15);
fgets(roomReserve[roomNo], 15, stdin);
//memset(reservationName[roomNo], 0, 15); //IS THIS NEEDED?
//fgets(reservationName[roomNo], 15, stdin);
// to remove new line character
printf("Room %d has been reserved for %s, Reservation Id: %d ", roomNo+1, reservationName, *reservationId);
// increment reservation Id
*reservationId+=1;
}
else {
char choice;
printf("There are no rooms available. Do you want to get registered in waiting list(y/n): ");
scanf_s(" %c", &choice,1);
// to consume extra new line
//getchar(); //USED SCAN INSTEAD
if (choice == 'Y' || choice == 'y')
{
//DISTINGUISHED BETWEEN RESERVATION ID AND WAITING ID
waiting[*waitingCount] = *waitingId;
printf("Enter your name for waiting list: ");
scanf_s(" %s", &waitingName[*waitingCount], 15);
// empty the string first
memset(waitingName[*waitingCount], 0, 15);
fgets(waitingName[*waitingCount], 15, stdin);
// to remove new line character
printf("You are successfully registered on waiting list with waitingId: %d name: %s ", waiting[*waitingCount], waitingName[*waitingCount]);
*waitingCount += 1;
*waitingId += 1;
}
else
printf("Select another option: ");
}
printf(" ");
}
// try to cancel a reservation if possible
void cancelReservation(int *rooms, char roomReserve[NO_OF_ROOMS][15], int *waiting, char waitingName[MAXWAITING][15], int noOfRooms, int *waitingCount, char *reservationName) {
printf(" Enter reservation Id to be cancelled: ");
int resId;
scanf_s("%d", &resId);
// to consume extra new line
//getchar(); // USED A SCANF INSTEAD
if (resId <= 0) {
printf("Reservation Id should be greater than 0");
return;
}
int roomToCancel = isResvIdPresent(rooms, noOfRooms, resId);
if (roomToCancel != -1)
{
// resv id is valid
printf(" ReservationId: %d has been cancelled successsfully.", resId);
rooms[resId] = rooms[roomToCancel];
int waitingIndex = isAnyoneWaiting(waiting, *waitingCount);
if (waitingIndex != -1)
{
//rooms[roomToCancel] = waiting[waitingIndex];
strcpy_s(roomReserve[roomToCancel], waitingName[*waitingCount]); //COPIES PERSON NEXT IN WAITLIST TO RECENTLY CANCELLED ROOM
printf(" Reservation Id: %d has been confirmed.", rooms[resId]);
removeWaiting(waiting, waitingName, *waitingCount, 0);
*waitingCount -= 1;
}
}
else {
printf(" Invalid reservation id to be cancelled");
}
printf(" ");
}
// try to cancel a waiting if possible
void cancelWaiting(int *waiting, char waitingName[MAXWAITING][15], int *waitingCount) {
printf(" Enter reservation Id to be cancelled in waiting list: ");
int resId;
scanf_s("%d", &resId);
// to consume extra new line
//getchar(); REPLACED WITH A SCAN
if (resId <= 0) {
printf("Reservation Id should be greater than 0");
return;
}
int resvIdTOCancel = isResvIdPresent(waiting, *waitingCount, resId);
if (resvIdTOCancel != -1) {
// if res id is valid
printf(" ReservationId: %d has been cancelled successsfully from waiting list. ", resId);
removeWaiting(waiting, waitingName, *waitingCount, resvIdTOCancel);
*waitingCount -= 1; //DECREMENTS PLACE IN LINE
}
else {
printf(" Invalid reservation id to be cancelled. Not on waiting list. ");
}
printf(" ");
}
// Print information related to waiting and reservation
void printInfo(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int waitingCount) {
int i = 0;
int noOfRoomReserved = 0;
for (; i<noOfRooms; i++) {
if (rooms[i] != -1) {
noOfRoomReserved++; //GOES THROUGH EACH ROOM
if (noOfRoomReserved == 1) {
printf(" Room Reservation List:");
}
printf(" RoomNo. %d, ReservationID: %d, Name: %s", i + 1, rooms[i], roomReserve[i]);
}
}
if (noOfRoomReserved <= 0) {
printf(" No room is reserved.");
}
if (waitingCount <= 0) {
printf(" No one is on waitlist.");
}
else {
printf(" Waiting List =>>>");
for (i = 0; i< waitingCount; i++) {
if (waiting[i] != -1) {
printf(" ReservationID: %d, Name: %s", waiting[i], waitingName[i]);
}
}
}
printf(" ");
}
int main(int argc, char **argv)
{
char choice;
int rooms[NO_OF_ROOMS], waiting[MAXWAITING], noOfRooms;
char roomReserve[NO_OF_ROOMS][15], waitingName[MAXWAITING][15], reservationName[NO_OF_ROOMS][maxName]; //ADDED RESERVATION NAME
int reservationId = 1, i, waitingCount = 0, waitingId = 1; // DISTINGUISHED BETWEEN WAITING ID AND RESERVATION ID
for (i = 0; i<MAXWAITING; ++i) {
waiting[i] = -1;
}
for (i = 0; i<NO_OF_ROOMS; ++i) {
rooms[i] = -1;
}
while (1) {
int exit = 0;
printf("Enter choice: 1.Reserve(R or r) 2.Cancel(C or c) 3.Remove From Waiting(W or w) 4.List reservations(L or l):");
scanf_s("%c", &choice,1);
switch (choice)
{
case 'R':
case 'r':
reserveRoomIfPossible(rooms, roomReserve, waiting, waitingName, noOfRooms, &reservationId, &waitingCount, *reservationName, &waitingId); //ADDED RESERVATION NAME AND WAITING ID
break;
case 'C':
case 'c':
cancelReservation(&rooms, roomReserve[NO_OF_ROOMS][15], &waiting, waitingName[MAXWAITING][15], noOfRooms, &waitingCount, &reservationName[NO_OF_ROOMS][15]);
break;
case 'W':
case 'w':
cancelWaiting(waiting, waitingName, &waitingCount);
break;
case 'L':
case 'l':
printInfo(rooms, roomReserve, waiting, waitingName, NO_OF_ROOMS, waitingCount);
break;
case 'Q':
case 'q':
exit = 1;
break;
default:
printf("Wrong choice. Please try again. ");
}
if (exit) {
printf("Exiting now.");
break;
}
}
}
Explanation / Answer
void removeWaiting(int *waiting, char waitingName[][15], int n, int indexToRemove) {
int lastWaitingId = waiting[0];
char * s = waitingName[0];
int i = indexToRemove;
for (; i<n - 1; i++) {
waiting[i] = waiting[i + 1];
memset(&waitingName[i], 0, strlen(waitingName[i]));
strncpy_s(waitingName[i], waitingName[i+1], sizeof(waitingName)); //COPIES NAME OF PERSON 2ND IN LINE TO 1ST IN LINE
}
waiting[i] = -1;
}
// try to reserve a room if possible
void reserveRoomIfPossible(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int *reservationId, int *waitingCount, char* reservationName, int *waitingId) {
int roomNo = isSomeRoomFree(rooms, noOfRooms);
if (roomNo != -1) {
int nameLength = sizeof(reservationName);
rooms[roomNo] = *reservationId;
printf("Room is available. ");
printf("Enter your name for reservation: "); //NAME FOR RESERVATION
scanf_s("%s", reservationName, nameLength); //FOR SOME REASON, HAVE TO PRESS ENTER IN THE COMMAND WINDOW THEN ENTER NAME, THEN HIT ENTER
// empty the string first
memset(roomReserve[roomNo], 0, 15);
fgets(roomReserve[roomNo], 15, stdin);
//memset(reservationName[roomNo], 0, 15); //IS THIS NEEDED?
//fgets(reservationName[roomNo], 15, stdin);
// to remove new line character
printf("Room %d has been reserved for %s, Reservation Id: %d ", roomNo+1, reservationName, *reservationId);
// increment reservation Id
*reservationId+=1;
}
else {
char choice;
printf("There are no rooms available. Do you want to get registered in waiting list(y/n): ");
scanf_s(" %c", &choice,1);
// to consume extra new line
//getchar(); //USED SCAN INSTEAD
if (choice == 'Y' || choice == 'y')
{
//DISTINGUISHED BETWEEN RESERVATION ID AND WAITING ID
waiting[*waitingCount] = *waitingId;
printf("Enter your name for waiting list: ");
scanf_s(" %s", &waitingName[*waitingCount], 15);
// empty the string first
memset(waitingName[*waitingCount], 0, 15);
fgets(waitingName[*waitingCount], 15, stdin);
// to remove new line character
printf("You are successfully registered on waiting list with waitingId: %d name: %s ", waiting[*waitingCount], waitingName[*waitingCount]);
*waitingCount += 1;
*waitingId += 1;
}
else
printf("Select another option: ");
}
printf(" ");
}
// try to cancel a reservation if possible
void cancelReservation(int *rooms, char roomReserve[NO_OF_ROOMS][15], int *waiting, char waitingName[MAXWAITING][15], int noOfRooms, int *waitingCount, char *reservationName) {
printf(" Enter reservation Id to be cancelled: ");
int resId;
scanf_s("%d", &resId);
// to consume extra new line
//getchar(); // USED A SCANF INSTEAD
if (resId <= 0) {
printf("Reservation Id should be greater than 0");
return;
}
int roomToCancel = isResvIdPresent(rooms, noOfRooms, resId);
if (roomToCancel != -1)
{
// resv id is valid
printf(" ReservationId: %d has been cancelled successsfully.", resId);
rooms[resId] = rooms[roomToCancel];
int waitingIndex = isAnyoneWaiting(waiting, *waitingCount);
if (waitingIndex != -1)
{
//rooms[roomToCancel] = waiting[waitingIndex];
strcpy_s(roomReserve[roomToCancel], waitingName[*waitingCount]); //COPIES PERSON NEXT IN WAITLIST TO RECENTLY CANCELLED ROOM
printf(" Reservation Id: %d has been confirmed.", rooms[resId]);
removeWaiting(waiting, waitingName, *waitingCount, 0);
*waitingCount -= 1;
}
}
else {
printf(" Invalid reservation id to be cancelled");
}
printf(" ");
}
// try to cancel a waiting if possible
void cancelWaiting(int *waiting, char waitingName[MAXWAITING][15], int *waitingCount) {
printf(" Enter reservation Id to be cancelled in waiting list: ");
int resId;
scanf_s("%d", &resId);
// to consume extra new line
//getchar(); REPLACED WITH A SCAN
if (resId <= 0) {
printf("Reservation Id should be greater than 0");
return;
}
int resvIdTOCancel = isResvIdPresent(waiting, *waitingCount, resId);
if (resvIdTOCancel != -1) {
// if res id is valid
printf(" ReservationId: %d has been cancelled successsfully from waiting list. ", resId);
removeWaiting(waiting, waitingName, *waitingCount, resvIdTOCancel);
*waitingCount -= 1; //DECREMENTS PLACE IN LINE
}
else {
printf(" Invalid reservation id to be cancelled. Not on waiting list. ");
}
printf(" ");
}
// Print information related to waiting and reservation
void printInfo(int *rooms, char roomReserve[][15], int *waiting, char waitingName[][15], int noOfRooms, int waitingCount) {
int i = 0;
int noOfRoomReserved = 0;
for (; i<noOfRooms; i++) {
if (rooms[i] != -1) {
noOfRoomReserved++; //GOES THROUGH EACH ROOM
if (noOfRoomReserved == 1) {
printf(" Room Reservation List:");
}
printf(" RoomNo. %d, ReservationID: %d, Name: %s", i + 1, rooms[i], roomReserve[i]);
}
}
if (noOfRoomReserved <= 0) {
printf(" No room is reserved.");
}
if (waitingCount <= 0) {
printf(" No one is on waitlist.");
}
else {
printf(" Waiting List =>>>");
for (i = 0; i< waitingCount; i++) {
if (waiting[i] != -1) {
printf(" ReservationID: %d, Name: %s", waiting[i], waitingName[i]);
}
}
}
printf(" ");
}
int main(int argc, char **argv)
{
char choice;
int rooms[NO_OF_ROOMS], waiting[MAXWAITING], noOfRooms;
char roomReserve[NO_OF_ROOMS][15], waitingName[MAXWAITING][15], reservationName[NO_OF_ROOMS][maxName]; //ADDED RESERVATION NAME
int reservationId = 1, i, waitingCount = 0, waitingId = 1; // DISTINGUISHED BETWEEN WAITING ID AND RESERVATION ID
for (i = 0; i<MAXWAITING; ++i) {
waiting[i] = -1;
}
for (i = 0; i<NO_OF_ROOMS; ++i) {
rooms[i] = -1;
}
while (1) {
int exit = 0;
printf("Enter choice: 1.Reserve(R or r) 2.Cancel(C or c) 3.Remove From Waiting(W or w) 4.List reservations(L or l):");
scanf_s("%c", &choice,1);
switch (choice)
{
case 'R':
case 'r':
reserveRoomIfPossible(rooms, roomReserve, waiting, waitingName, noOfRooms, &reservationId, &waitingCount, *reservationName, &waitingId); //ADDED RESERVATION NAME AND WAITING ID
break;
case 'C':
case 'c':
cancelReservation(&rooms, roomReserve[NO_OF_ROOMS][15], &waiting, waitingName[MAXWAITING][15], noOfRooms, &waitingCount, &reservationName[NO_OF_ROOMS][15]);
break;
case 'W':
case 'w':
cancelWaiting(waiting, waitingName, &waitingCount);
break;
case 'L':
case 'l':
printInfo(rooms, roomReserve, waiting, waitingName, NO_OF_ROOMS, waitingCount);
break;
case 'Q':
case 'q':
exit = 1;
break;
default:
printf("Wrong choice. Please try again. ");
}
if (exit) {
printf("Exiting now.");
break;
}
}
}