m3.cpp
1 /* 2 * FILE : m3.cpp 3 * PROJECT : SENG1000 - MAJOR ASSIGNMENT 3 4 * PROGRAMMER : Tian Yang, 8952896 5 * FIRST VERSION : 2024-02-15 6 * DESCRIPTION : 7 * This program calculate hotel bills for a meeting. 8 */ 9 10 #include <stdio.h> 11 #include <string.h> 12 13 #define BUFFER_SIZE 1024 14 #define NAME_LENGTH 32 15 16 const int kError = -1; 17 const char* kWeekdays[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; 18 19 int indexTheDay(const char*); 20 double calculateCostOfRoom(const double[], int, int); 21 22 int main(void) 23 { 24 const double kRoomRate[] = { 137.50, 138.25, 130.50, 150, 150, 162.50 }; 25 const int kCountOfPeople = 4; 26 double total = 0.0; 27 for (int i = 0; i < kCountOfPeople; i++) 28 { 29 char buffer[BUFFER_SIZE] = ""; 30 char name[NAME_LENGTH] = ""; 31 printf("Enter Name: "); 32 fgets(buffer, BUFFER_SIZE, stdin); 33 sscanf(buffer, "%[^\n]", name); 34 if (name[0] == '\0') 35 { 36 printf("Invalid name entry. Moving on to next guest...\n"); 37 printf("\n"); 38 continue; 39 } 40 41 char checkInDay[4] = ""; 42 int checkIn = kError; 43 printf("Enter check-in day: "); 44 fgets(buffer, BUFFER_SIZE, stdin); 45 sscanf(buffer, "%s\n", checkInDay); 46 checkIn = indexTheDay(checkInDay); 47 if (checkIn == kError) 48 { 49 printf("Invalid check-in day. Moving on to next guest...\n"); 50 printf("\n"); 51 continue; 52 } 53 if ((checkIn > 4)) // Index of Thursday. 54 { 55 printf("This person is missing the meeting. Moving on to next guest...\n"); 56 printf("\n"); 57 continue; 58 } 59 60 char checkOutDay[4] = ""; 61 int checkOut = kError; 62 printf("Enter check-out day: "); 63 fgets(buffer, BUFFER_SIZE, stdin); 64 sscanf(buffer, "%s\n", checkOutDay); 65 checkOut = indexTheDay(checkOutDay); 66 if(checkOut == kError) 67 { 68 printf("Invalid check-out day. Moving on to next guest...\n"); 69 printf("\n"); 70 continue; 71 } 72 if (checkOut < 4) // Index of Thursday. 73 { 74 printf("This person is missing the meeting. Moving on to next guest...\n"); 75 printf("\n"); 76 continue; 77 } 78 79 if ((checkOut - checkIn) < 1) 80 { 81 printf("Invalid length of stay. Moving on to next guest...\n"); 82 printf("\n"); 83 continue; 84 } 85 86 double cost = calculateCostOfRoom(kRoomRate, checkIn, checkOut); 87 printf("The total room cost for %s is %.2f\n", name, cost); 88 printf("\n"); 89 total += cost; 90 } 91 printf("Grand total : %.2f", total); 92 93 return 0; 94 } 95 96 // 97 // FUNCTION : indexTheDay 98 // DESCRIPTION : 99 // This function converts from the user input to a useful array index 100 // PARAMETERS : 101 // const char* day : day string. 102 // RETURNS : 103 // int : index of day. 104 // 105 int indexTheDay(const char* day) 106 { 107 for (int i = 0; i < 7; i++) 108 { 109 if (strcmp(day, kWeekdays[i]) == 0) 110 return i; 111 } 112 return kError; 113 } 114 115 // 116 // FUNCTION : calculateCostOfRoom 117 // DESCRIPTION : 118 // This function calculates the cost of the room for a person. 119 // PARAMETERS : 120 // const double roomRate[] : room rate. 121 // int checkIn: check-in day. 122 // int checkOut: check-out day. 123 // RETURNS : 124 // double : total cost of the room. 125 // 126 double calculateCostOfRoom(const double roomRate[], int checkIn, int checkOut) 127 { 128 double cost = 0.0; 129 for (int i = checkIn; i < checkOut; i++) 130 { 131 cost += roomRate[i]; 132 } 133 return cost; 134 }