What's new

C language

Bossy_13

Addict
Established
Joined
Jan 9, 2019
Posts
16
Reaction
2
Points
73
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
 

Attachments

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.
}
 
chatgpt is the key

C:
#include <stdio.h>
#include <stdlib.h>

void clrscr() {
    system("cls");  // For Windows, change to "clear" for Linux
}

void divider() {
    printf("\n--------------------------------\n");
}

void title(const char *str) {
    divider();
    printf("     %s", str);
    divider();
}

void printPrice(double totalP, int totalI) {
    divider();
    printf("Total items : %d\n", totalI);
    printf("Total price : php%g", totalP);
    divider();
}

void invalid() {
    divider();
    printf(" Invalid choice !");
}

int askChoice() {
    int ch;
    printf("\nChoice : ");
    scanf("%d", &ch);
    return ch;
}

void processMenu(const char *menuTitle, const char **menuItems, const double *prices, int menuSize,
                 double *totalPrice, int *totalItems) {
    clrscr();
    title(menuTitle);

    for (int i = 0; i < menuSize; i++) {
        printf(" (%d) %s : php%g\n", i + 1, menuItems[i], prices[i]);
    }

    printf(" (%d) Back\n", menuSize + 1);
    printPrice(*totalPrice, *totalItems);

    int choice = askChoice();

    if (choice < 1 || choice > menuSize + 1) {
        clrscr();
        invalid();
    } else if (choice == menuSize + 1) {
        clrscr();
        // Do nothing, let it go back to the main menu
    } else {
        *totalPrice += prices[choice - 1];
        *totalItems += 1;
        clrscr();
        printf("You've added %s to your order.\n", menuItems[choice - 1]);
        // Additional processing can be added here if needed
    }
}

int main() {
    double totalPrice = 0.0;
    int totalItems = 0;

    int mainMenuItems = 6;
    const char *mainMenu[] = {
        "Buy 1 take 1", "Single Order", "Beverages", "Check out", "Reset", "Exit"
    };

    int quit = 0;

    while (!quit) {
        clrscr();
        title("Thai me UP");

        for (int i = 0; i < mainMenuItems; i++) {
            printf(" (%d): %s\n", i + 1, mainMenu[i]);
        }

        printPrice(totalPrice, totalItems);

        int choice = askChoice();

        switch (choice) {
            case 1:
                processMenu("Buy 1 Take 1", (const char *[]){"Burger", "Cheese Burger", "Hotdog sandwich"},
                             (const double[]){40, 50, 50}, 3, &totalPrice, &totalItems);
                break;
            case 2:
                processMenu("Single Order", (const char *[]){"Burger with egg", "Burger with ham", "Cheese burger with egg", "Double patty"},
                             (const double[]){35, 35, 40, 40}, 4, &totalPrice, &totalItems);
                break;
            case 3:
                processMenu("Beverages", (const char *[]){"Mineral Water", "Coke", "Mountain Dew"},
                             (const double[]){15, 22, 22}, 3, &totalPrice, &totalItems);
                break;
            case 4:
                clrscr();
                title("Check out");
                printf("\nYour order will be arrived shortly.\n");
                printPrice(totalPrice, totalItems);
                break;
            case 5:
                totalPrice = 0.0;
                totalItems = 0;
                clrscr();
                break;
            case 6:
                quit = 1;
                break;
            default:
                clrscr();
                invalid();
        }
    }

    return 0;
}
 
Last edited:

Similar threads

Back
Top