fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define MAX_SEATS 30
  6. #define MAX_MOVIES 4
  7. #define MAX_BOOKING 4
  8. #define rows 5
  9. #define cols 6
  10.  
  11. //Ticket
  12. struct Ticket {
  13. char customerName[100];
  14. int booked;
  15. };
  16.  
  17. //Movie
  18. struct Movie {
  19. char movieName[50];
  20. struct Ticket seats[MAX_SEATS];
  21. };
  22. struct Movie movies[MAX_MOVIES] = {{"CPE Adventure"},{"Mufasa: The Lion King"},{"Ratatouille"},{"Avengers: Endgame"}};
  23.  
  24. //Movie list
  25. void displayMovies(struct Movie movies[]) {
  26. printf("\n-- Movies list --\n");
  27. for (int i = 0; i < MAX_MOVIES; i++) {
  28. printf("\n%d. %s\n", i + 1, movies[i].movieName);
  29. }
  30. }
  31.  
  32. // Seat booking
  33. void bookTicket(struct Movie *movie) {
  34. int col;
  35. int numtickets;
  36. char name[50];
  37. char rowchar;
  38.  
  39. //Display seat
  40. for (int row = 0; row < rows; row++) {
  41. for (int col = 0; col < cols; col++) {
  42. int seatIndex = row * cols + col;
  43. if (seatIndex >= MAX_SEATS) break;
  44.  
  45. char rowChar = 'A' + row;
  46. printf("%c%d(", rowChar, col + 1);
  47. if (movie->seats[seatIndex].booked) printf("XX");
  48. else printf("");
  49. printf(")\t");
  50. }
  51. printf("\n\n");
  52. }
  53.  
  54. printf("How many tickets do you want : ");
  55. scanf("%d", &numtickets);
  56.  
  57. if (numtickets < 1 || numtickets > MAX_SEATS) {
  58. printf("Invalid number of seats!\n");
  59. return;
  60. }
  61.  
  62. printf("Enter your name : ");
  63. scanf(" %[^\n]", name); //C B8B#C B8B1C B8C B8C B9C B8B2C B8C B8B7C B9C B8
  64.  
  65. for (int i = 0; i < numtickets; i++) {
  66. printf("\nBooking seat %d \n", i + 1);
  67.  
  68. printf("Enter seat (Ex:A1): ");
  69. scanf(" %c%d", &rowchar, &col);
  70. rowchar = toupper(rowchar);
  71.  
  72. int row = rowchar - 'A';
  73. if (row < 0 || row >= rows || col < 1 || col > cols) {
  74. printf("\nInvalid seat!\n");
  75. continue;
  76. }
  77.  
  78. int seatIndex = row * cols + (col - 1);
  79. if (movie->seats[seatIndex].booked) {
  80. printf("\nSeat %c%d already booked!\n", rowchar, col);
  81. continue;
  82. }
  83.  
  84. movie->seats[seatIndex].booked = 1;
  85. strcpy(movie->seats[seatIndex].customerName, name);
  86.  
  87. printf("Seat %c%d reserved for %s\n", rowchar, col, name);
  88. }
  89. }
  90.  
  91. //Cancel booking
  92. void cancelBooking(struct Movie *movie) {
  93. int col;
  94. int numtickets;
  95. char name[50];
  96. char rowchar;
  97.  
  98. //Display seat
  99. for (int row = 0; row < rows; row++) {
  100. for (int col = 0; col < cols; col++) {
  101. int seatIndex = row * cols + col;
  102. if (seatIndex >= MAX_SEATS) {
  103. break;
  104. }
  105.  
  106. char rowChar = 'A' + row; // C B9C B8C B8B%C B8C B9C B8B%C B8C B9C B8C B8B'C B9C B8C B9C B8C B8C B8B1C B8B'C B8 C B8B1C B8C B8B)C B8B# A, B, C, ...
  107.  
  108. printf("%c%d(", rowChar, col + 1);
  109.  
  110. if (movie->seats[seatIndex].booked) {
  111. printf("XX");
  112. }
  113. else {
  114. printf("");
  115. }
  116. printf(")\t");
  117. }
  118. printf("\n\n"); // C B8C B8B6C B9C B8C B8C B8B#C B8B#C B8C B8B1C B8C B9C B8B+C B8B!C B9C B9C B8B!C B8B7C B9C B8 C B8C B8 1 C B9C B8C B8B'
  119. }
  120.  
  121. printf("\nPlease enter seat number to cancel (Ex : A1): \n");
  122. scanf(" %c%d", &rowchar, &col);
  123. rowchar = toupper(rowchar);
  124.  
  125.  
  126. int row = rowchar - 'A';
  127. if (row < 0 || row >= rows || col < 1 || col > cols) {
  128. printf("Invalid seat!\n");
  129. return;
  130. }
  131.  
  132. int seatIndex = row * cols + (col - 1);
  133. if (movie->seats[seatIndex].booked == 0) {
  134. printf("Seat is available!\n");
  135. return;
  136. }
  137. else {
  138. movie->seats[seatIndex].booked = 0;
  139. strcpy(movie->seats[seatIndex].customerName, ""); // C B8B%C B8C B8C B8B7C B9C B8 C B8B%C B8B9C B8C B8C B9C B8B2
  140. printf("Seat %c%d successfully cancel\n", rowchar, col);
  141. }
  142. }
  143.  
  144. //Seat available
  145. void checkSeatStatus(struct Movie *movie) {
  146. printf("\nCheck seat available (%s)\n\n", movie->movieName);
  147.  
  148. //Display seat
  149. for (int row = 0; row < rows; row++) {
  150. for (int col = 0; col < cols; col++) {
  151. int seatIndex = row * cols + col;
  152. if (seatIndex >= MAX_SEATS) {
  153. break;
  154. }
  155.  
  156. char rowChar = 'A' + row; // C B9C B8C B8B%C B8C B9C B8B%C B8C B9C B8C B8B'C B9C B8C B9C B8C B8C B8B1C B8B'C B8 C B8B1C B8C B8B)C B8B# A, B, C, ...
  157.  
  158. printf("%c%d(", rowChar, col + 1);
  159.  
  160. if (movie->seats[seatIndex].booked) {
  161. printf("XX");
  162. }
  163. else {
  164. printf("");
  165. }
  166. printf(")\t");
  167. }
  168. printf("\n\n"); // C B8C B8B6C B9C B8C B8C B8B#C B8B#C B8C B8B1C B8C B9C B8B+C B8B!C B9C B9C B8B!C B8B7C B9C B8 C B8C B8 1 C B9C B8C B8B'
  169. }
  170. }
  171.  
  172. // Search bookings
  173. void searchBooking(struct Movie movies[]) {
  174. int found = 0;
  175. char searchName[100];
  176.  
  177. printf("Enter customer name to search : ");
  178. scanf(" %[^\n]", searchName);
  179.  
  180. printf("\n--- Bookings for | %s | ---\n", searchName);
  181.  
  182. for (int i = 0; i < MAX_MOVIES; i++) {
  183. for (int j = 0; j < MAX_SEATS; j++) {
  184. if (movies[i].seats[j].booked && strcmp(movies[i].seats[j].customerName, searchName) == 0) {
  185.  
  186. int row = j / cols;
  187. int col = j % cols;
  188.  
  189. printf("\n------------------------------\n");
  190. printf("\nMovie: %s \n", movies[i].movieName);
  191. printf("Seat: %c%d\n ", 'A' + row, col + 1);
  192. printf("\n------------------------------\n");
  193. found = 1;
  194. }
  195. }
  196. }
  197. if (!found) {
  198. printf("No bookings found for | %s |.\n", searchName);
  199. }
  200. }
  201.  
  202. //Save data
  203. void saveData(struct Movie movies[]) {
  204. FILE *file = fopen("ticketfinal.txt", "w");//C B9C B8C B8C B9C B8C B9C B8C B8B%C B9C B8C B8B7C B9C B8 ticketfinal.txt
  205. if (file == NULL) {
  206. printf("Error opening file for writing!\n");
  207. return;
  208. }
  209.  
  210. for (int i = 0; i < MAX_MOVIES; i++) {
  211. fprintf(file, "Movie: %s\n", movies[i].movieName);
  212. for (int j = 0; j < MAX_SEATS; j++) {
  213. if (movies[i].seats[j].booked) {
  214. fprintf(file, "Seat %d: Booked by %s\n", j + 1, movies[i].seats[j].customerName);
  215. }
  216. else {
  217. fprintf(file, "Seat %d: Free\n", j + 1);
  218. }
  219. }
  220. fprintf(file, "------------------------------------------------------\n");
  221. }//C B9C B8C B9C B8C B8C B8B1C B8B'C B8C B8B1C B9C B8C B8B#C B8B0C B8B+C B8B'C B9C B8B2C B8C B9C B8C B9C B8B%C B8B0C B9C B8B#C B8B7C B9C B8 C B8
  222.  
  223. printf("Data saved successfully to ticketfinal.txt!\n");
  224. }
  225. // Load data
  226. void loadData(struct Movie movies[]) {
  227. FILE *file = fopen("ticketfinal.txt", "r");
  228. if (file == NULL) {
  229. printf("");
  230. return;
  231. }
  232.  
  233. char line[200];
  234. int movieIndex = -1;
  235. while (fgets(line, sizeof(line), file)) {
  236. if (strncmp(line, "Movie:", 6) == 0) {
  237. movieIndex++;
  238. }
  239. else if (strncmp(line, "Seat", 4) == 0 && movieIndex >= 0) {
  240. int seatNumber;
  241. char status[20];
  242. char name[100] = "";
  243.  
  244. if (strstr(line, "Booked by")) {
  245. sscanf(line, "Seat %d: Booked by %s", &seatNumber, name);
  246. movies[movieIndex].seats[seatNumber - 1].booked = 1;
  247. strcpy(movies[movieIndex].seats[seatNumber - 1].customerName, name);
  248. }
  249. else {
  250. sscanf(line, "Seat %d: %s", &seatNumber, status);
  251. movies[movieIndex].seats[seatNumber - 1].booked = 0;
  252. strcpy(movies[movieIndex].seats[seatNumber - 1].customerName, "");
  253. }
  254. }
  255. }
  256. }
  257.  
  258. int main() {
  259. int choice;
  260. int movieChoice;
  261.  
  262. printf("Majoi movies ticket booking system\n");
  263. loadData(movies); //C B9C B8B+C B8B%C B8C B8C B9C B8 C B8B!C B8B9C B8B%C B8C B8B2C B8C B9C B8C B8B%C B9C B8C B8B5C B9C B8C B8B1C B8C B8C B8B6C B8C B9C B8B'C B9
  264.  
  265. while (1) {
  266. printf("\n| -- Main menu -- |\n| |\n| 1. Ticket booking |");
  267. printf("\n| 2. Seat available |\n| 3. Cancel booking |\n| 4. search booking |");
  268. printf("\n| 5. Exit |\nSelect menu (1-4): ");
  269. scanf("%d", &choice);
  270.  
  271. switch (choice) {
  272. case 1:
  273. displayMovies(movies);
  274. printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
  275. scanf("%d", &movieChoice);
  276. if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
  277. printf("\nSelection failed!\n");
  278. }
  279. else {
  280. bookTicket(&movies[movieChoice - 1]);
  281. }
  282. break;
  283.  
  284. case 2:
  285. displayMovies(movies);
  286. printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
  287. scanf("%d", &movieChoice);
  288. if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
  289. printf("\nSelection failed!\n");
  290. }
  291. else {
  292. checkSeatStatus(&movies[movieChoice - 1]);
  293. }
  294. break;
  295.  
  296. case 3:
  297. displayMovies(movies);
  298. printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
  299. scanf("%d", &movieChoice);
  300. if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
  301. printf("\nSelection failed!\n");
  302. }
  303. else {
  304. cancelBooking(&movies[movieChoice - 1]);
  305. }
  306. break;
  307.  
  308. case 4:
  309. searchBooking(movies);
  310. break;
  311.  
  312. case 5:
  313. saveData(movies);//C B9C B8C B8C B8C B9C B8 C B8B!C B8B9C B8B%
  314. printf("\nThank you Jubjub!\n");
  315. return 0;
  316.  
  317. default:
  318. printf("\nPlease select menu again (1-4)\n");
  319. }
  320. }
  321. }
Success #stdin #stdout 0.03s 25180KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_SEATS 30
#define MAX_MOVIES 4
#define MAX_BOOKING 4
#define rows 5
#define cols 6

//Ticket
struct Ticket {
 char customerName[100];
 int booked;
};

//Movie
struct Movie {
 char movieName[50];
 struct Ticket seats[MAX_SEATS];
};
struct Movie movies[MAX_MOVIES] = {{"CPE Adventure"},{"Mufasa: The Lion King"},{"Ratatouille"},{"Avengers: Endgame"}};

//Movie list
void displayMovies(struct Movie movies[]) {
 printf("\n-- Movies list --\n");
 for (int i = 0; i < MAX_MOVIES; i++) {
  printf("\n%d. %s\n", i + 1, movies[i].movieName);
 }
}

// Seat booking
void bookTicket(struct Movie *movie) {
 int col;
 int numtickets;
 char name[50];
 char rowchar;

 //Display seat
 for (int row = 0; row < rows; row++) {
  for (int col = 0; col < cols; col++) {
   int seatIndex = row * cols + col;
   if (seatIndex >= MAX_SEATS) break;

   char rowChar = 'A' + row;
   printf("%c%d(", rowChar, col + 1);
   if (movie->seats[seatIndex].booked) printf("XX");
   else printf("");
   printf(")\t");
  }
  printf("\n\n");
 }

 printf("How many tickets do you want : ");
 scanf("%d", &numtickets);

 if (numtickets < 1 || numtickets > MAX_SEATS) {
  printf("Invalid number of seats!\n");
  return;
 }

 printf("Enter your name : ");
 scanf(" %[^\n]", name);  //C B8B#C B8B1C B8C B8C B9C B8B2C B8C B8B7C B9C B8

 for (int i = 0; i < numtickets; i++) {
  printf("\nBooking seat %d \n", i + 1);

  printf("Enter seat (Ex:A1): ");
  scanf(" %c%d", &rowchar, &col);
  rowchar = toupper(rowchar);

  int row = rowchar - 'A';
  if (row < 0 || row >= rows || col < 1 || col > cols) {
   printf("\nInvalid seat!\n");
   continue;
  }

  int seatIndex = row * cols + (col - 1);
  if (movie->seats[seatIndex].booked) {
   printf("\nSeat %c%d already booked!\n", rowchar, col);
   continue;
  }

  movie->seats[seatIndex].booked = 1;
  strcpy(movie->seats[seatIndex].customerName, name);

  printf("Seat %c%d reserved for %s\n", rowchar, col, name);
 }
}

//Cancel booking
void cancelBooking(struct Movie *movie) {
 int col;
 int numtickets;
 char name[50];
 char rowchar;

 //Display seat
 for (int row = 0; row < rows; row++) {
  for (int col = 0; col < cols; col++) {
   int seatIndex = row * cols + col;
   if (seatIndex >= MAX_SEATS) {
    break;
   }

   char rowChar = 'A' + row;  // C B9C B8C B8B%C B8C B9C B8B%C B8C B9C B8C B8B'C B9C B8C B9C B8C B8C B8B1C B8B'C B8 C B8B1C B8C B8B)C B8B# A, B, C, ...

   printf("%c%d(", rowChar, col + 1);

   if (movie->seats[seatIndex].booked) {
    printf("XX");
   }
   else {
    printf("");
   }
   printf(")\t");
  }
  printf("\n\n"); // C B8C B8B6C B9C B8C B8C B8B#C B8B#C B8C B8B1C B8C B9C B8B+C B8B!C B9C B9C B8B!C B8B7C B9C B8 C B8C B8 1 C B9C B8C B8B'
 }

 printf("\nPlease enter seat number to cancel (Ex : A1): \n");
 scanf(" %c%d", &rowchar, &col);
 rowchar = toupper(rowchar);


 int row = rowchar - 'A';
 if (row < 0 || row >= rows || col < 1 || col > cols) {
  printf("Invalid seat!\n");
  return;
 }

 int seatIndex = row * cols + (col - 1);
 if (movie->seats[seatIndex].booked == 0) {
  printf("Seat is available!\n");
  return;
 }
 else {
  movie->seats[seatIndex].booked = 0;
  strcpy(movie->seats[seatIndex].customerName, ""); // C B8B%C B8C B8C B8B7C B9C B8 C B8B%C B8B9C B8C B8C B9C B8B2
  printf("Seat %c%d successfully cancel\n", rowchar, col);
 }
}

//Seat available
void checkSeatStatus(struct Movie *movie) {
 printf("\nCheck seat available (%s)\n\n", movie->movieName);

 //Display seat
 for (int row = 0; row < rows; row++) {
  for (int col = 0; col < cols; col++) {
   int seatIndex = row * cols + col;
   if (seatIndex >= MAX_SEATS) {
    break;
   }

   char rowChar = 'A' + row;  // C B9C B8C B8B%C B8C B9C B8B%C B8C B9C B8C B8B'C B9C B8C B9C B8C B8C B8B1C B8B'C B8 C B8B1C B8C B8B)C B8B# A, B, C, ...

   printf("%c%d(", rowChar, col + 1);

   if (movie->seats[seatIndex].booked) {
    printf("XX");
   }
   else {
    printf("");
   }
   printf(")\t");
  }
  printf("\n\n"); // C B8C B8B6C B9C B8C B8C B8B#C B8B#C B8C B8B1C B8C B9C B8B+C B8B!C B9C B9C B8B!C B8B7C B9C B8 C B8C B8 1 C B9C B8C B8B'
 }
}

// Search bookings
void searchBooking(struct Movie movies[]) {
 int found = 0;
 char searchName[100];

 printf("Enter customer name to search : ");
 scanf(" %[^\n]", searchName);

 printf("\n--- Bookings for | %s | ---\n", searchName);

 for (int i = 0; i < MAX_MOVIES; i++) {
  for (int j = 0; j < MAX_SEATS; j++) {
   if (movies[i].seats[j].booked && strcmp(movies[i].seats[j].customerName, searchName) == 0) {

    int row = j / cols;
    int col = j % cols;

    printf("\n------------------------------\n");
    printf("\nMovie: %s \n", movies[i].movieName);
    printf("Seat: %c%d\n ", 'A' + row, col + 1);
    printf("\n------------------------------\n");
    found = 1;
   }
  }
 }
 if (!found) {
  printf("No bookings found for | %s |.\n", searchName);
 }
}

//Save data
void saveData(struct Movie movies[]) {
 FILE *file = fopen("ticketfinal.txt", "w");//C B9C B8C B8C B9C B8C B9C B8C B8B%C B9C B8C B8B7C B9C B8 ticketfinal.txt
 if (file == NULL) {
  printf("Error opening file for writing!\n");
  return;
 }

 for (int i = 0; i < MAX_MOVIES; i++) {
  fprintf(file, "Movie: %s\n", movies[i].movieName);
  for (int j = 0; j < MAX_SEATS; j++) {
   if (movies[i].seats[j].booked) {
    fprintf(file, "Seat %d: Booked by %s\n", j + 1, movies[i].seats[j].customerName);
   }
   else {
    fprintf(file, "Seat %d: Free\n", j + 1);
   }
  }
  fprintf(file, "------------------------------------------------------\n");
 }//C B9C B8C B9C B8C B8C B8B1C B8B'C B8C B8B1C B9C B8C B8B#C B8B0C B8B+C B8B'C B9C B8B2C B8C B9C B8C B9C B8B%C B8B0C B9C B8B#C B8B7C B9C B8 C B8

 fclose(file);
 printf("Data saved successfully to ticketfinal.txt!\n");
}
// Load data
void loadData(struct Movie movies[]) {
 FILE *file = fopen("ticketfinal.txt", "r");
 if (file == NULL) {
  printf("");
  return;
 }

 char line[200];
 int movieIndex = -1;
 while (fgets(line, sizeof(line), file)) {
  if (strncmp(line, "Movie:", 6) == 0) {
   movieIndex++;
  }
  else if (strncmp(line, "Seat", 4) == 0 && movieIndex >= 0) {
   int seatNumber;
   char status[20];
   char name[100] = "";

   if (strstr(line, "Booked by")) {
    sscanf(line, "Seat %d: Booked by %s", &seatNumber, name);
    movies[movieIndex].seats[seatNumber - 1].booked = 1;
    strcpy(movies[movieIndex].seats[seatNumber - 1].customerName, name);
   }
   else {
    sscanf(line, "Seat %d: %s", &seatNumber, status);
    movies[movieIndex].seats[seatNumber - 1].booked = 0;
    strcpy(movies[movieIndex].seats[seatNumber - 1].customerName, "");
   }
  }
 }
 fclose(file);
}

int main() {
 int choice;
 int movieChoice;

 printf("Majoi movies ticket booking system\n");
 loadData(movies); //C B9C B8B+C B8B%C B8C B8C B9C B8 C B8B!C B8B9C B8B%C B8C B8B2C B8C B9C B8C B8B%C B9C B8C B8B5C B9C B8C B8B1C B8C B8C B8B6C B8C B9C B8B'C B9

 while (1) {
  printf("\n|  -- Main menu --  |\n|                   |\n| 1. Ticket booking |");
  printf("\n| 2. Seat available |\n| 3. Cancel booking |\n| 4. search booking |");
  printf("\n| 5. Exit           |\nSelect menu (1-4): ");
  scanf("%d", &choice);

  switch (choice) {
  case 1:
   displayMovies(movies);
   printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
   scanf("%d", &movieChoice);
   if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
    printf("\nSelection failed!\n");
   }
   else {
    bookTicket(&movies[movieChoice - 1]);
   }
   break;

  case 2:
   displayMovies(movies);
   printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
   scanf("%d", &movieChoice);
   if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
    printf("\nSelection failed!\n");
   }
   else {
    checkSeatStatus(&movies[movieChoice - 1]);
   }
   break;

  case 3:
   displayMovies(movies);
   printf("\nSelect movie (1-%d): \n", MAX_MOVIES);
   scanf("%d", &movieChoice);
   if (movieChoice < 1 || movieChoice > MAX_MOVIES) {
    printf("\nSelection failed!\n");
   }
   else {
    cancelBooking(&movies[movieChoice - 1]);
   }
   break;

  case 4:
   searchBooking(movies);
   break;

  case 5:
   saveData(movies);//C B9C B8C B8C B8C B9C B8 C B8B!C B8B9C B8B%
   printf("\nThank you Jubjub!\n");
   return 0;

  default:
   printf("\nPlease select menu again (1-4)\n");
  }
 }
}