Can someone help me to finish a a C language code please. It's a simple Ordering menu, I have lackings in the code that's is the receipt in the code the attached file is the code
Did a refactor of your code. Also added comments to explain what each code does.
C:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to clear the terminal screen based on the operating system.
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// Function to print a divider for better readability in the menu.
void printDivider() {
printf("\n--------------------------------\n");
}
// Function to print a header for each section.
void printHeader(const char *header) {
printDivider();
printf(" %s\n", header);
printDivider();
}
// Function to print the menu and its items along with their prices.
void printMenu(const char **items, const double *prices, int count) {
for (int i = 0; i < count; i++) {
if (prices != NULL) {
printf(" (%d) %s : php%.2f\n", i + 1, items[i], prices[i]);
} else {
printf(" (%d) %s\n", i + 1, items[i]);
}
}
printf(" (%d) Back\n", count + 1);
}
// Function to get user's choice from the menu and validate it.
int getUserChoice(int itemCount) {
int choice;
printf("\nChoice: ");
scanf("%d", &choice);
while (choice < 1 || choice > itemCount + 1) {
printf("Invalid choice, try again: ");
scanf("%d", &choice);
}
return choice;
}
// Function to confirm checkout from the user.
char confirmCheckout() {
char confirm;
printf("\nConfirm Checkout? (Y/N): ");
scanf(" %c", &confirm);
return confirm;
}
// Function to print a receipt showing total items and total price.
void printReceipt(int totalItems, double totalPrice) {
clearScreen();
printHeader("Receipt");
printf("\nTotal items: %d\nTotal price: php%.2f\n", totalItems, totalPrice);
printDivider();
printf("\nThank you for your purchase!\n");
}
// Function to process an order, including adding items to the total and showing current total.
void processOrder(const char *title, const char **items, const double *prices, int itemCount, double *totalPrice, int *totalItems) {
int choice = 0;
while (choice != itemCount + 1) {
clearScreen();
printHeader(title);
printMenu(items, prices, itemCount);
printf("Total price: php%.2f\n", *totalPrice);
printDivider();
choice = getUserChoice(itemCount);
if (choice >= 1 && choice <= itemCount) {
*totalPrice += prices[choice - 1];
(*totalItems)++;
}
}
}
int main() {
double totalPrice = 0.0; // Variable to keep track of the total price.
int totalItems = 0; // Variable to keep track of the total number of items.
// Array of strings representing the main menu items.
const char *mainMenu[] = {"Buy 1 take 1", "Single Order", "Beverages", "Check out", "Reset", "Exit"};
// Calculate the number of items in the main menu.
int mainMenuItems = sizeof(mainMenu) / sizeof(mainMenu[0]);
bool running = true; // A flag to keep the program running until the user decides to exit.
while (running) { // Main loop to show the menu and process user input.
clearScreen();
printHeader("Thai me UP");
printMenu(mainMenu, NULL, mainMenuItems);
printf("Total items: %d\nTotal price: php%.2f\n", totalItems, totalPrice);
printDivider();
int choice = getUserChoice(mainMenuItems);
switch (choice) { // Handle the user's choice.
case 1: // Cases for ordering items.
case 2:
case 3: {
// Process orders for Buy 1 Take 1, Single Order, and Beverages.
// Similar process for all, just different items and prices.
processOrder(mainMenu[choice - 1], NULL, NULL, 0, &totalPrice, &totalItems);
break;
}
case 4: { // Checkout case.
if (totalItems > 0 && confirmCheckout() == 'Y') {
printReceipt(totalItems, totalPrice);
totalPrice = 0.0; // Reset the total price after checkout.
totalItems = 0; // Reset the total items after checkout.
} else {
printf("\nCheckout canceled. Returning to main menu.\n");
}
break;
}
case 5: { // Reset case.
totalPrice = 0.0; // Reset the total price.
totalItems = 0; // Reset the total items.
break;
}
case 6: { // Exit case.
running = false; // Set running to false to exit the program.
break;
}
}
if (choice != 6) {
printf("\nPress enter to continue...");
while (getchar() != '\n'); // Clear input buffer.
getchar(); // Wait for enter.
}
}
return 0; // End of the program.
}