Saturday, August 22, 2026

C program to print invoice

 #include <stdio.h>

#include <string.h>

#include <ctype.h>


#define MAX_ITEMS 5

#define MAX_NAME_LEN 20

#define MAX_CART 10


// Function to convert a string to lowercase for case-insensitive matching

void to_lowercase(char str[]) {

    for (int i = 0; str[i]; i++) {

        str[i] = tolower((unsigned char)str[i]);

    }

}


int main() {

    // Inventory data arrays

    char inventory_items[MAX_ITEMS][MAX_NAME_LEN] = {"apple", "bread", "milk", "eggs", "cheese"};

    double inventory_prices[MAX_ITEMS] = {1.50, 2.50, 3.20, 0.25, 4.00};


    // User cart arrays

    char cart_items[MAX_CART][MAX_NAME_LEN];

    int cart_quantities[MAX_CART];

    double cart_prices[MAX_CART];

    int cart_count = 0;


    char choice;

    

    printf("--- Welcome to the Store ---\n");

    printf("Available items: apple ($1.50), bread ($2.50), milk ($3.20), eggs ($0.25), cheese ($4.00)\n\n");


    // Shopping loop

    do {

        char input_item[MAX_NAME_LEN];

        char search_item[MAX_NAME_LEN];

        int quantity;

        int found = 0;


        printf("Enter item name to buy: ");

        scanf("%s", input_item);

        

        // Copy and convert to lowercase for flexible matching

        strcpy(search_item, input_item);

        to_lowercase(search_item);


        // Search inventory

        for (int i = 0; i < MAX_ITEMS; i++) {

            if (strcmp(search_item, inventory_items[i]) == 0) {

                found = 1;

                printf("Enter quantity: ");

                scanf("%d", &quantity);


                if (quantity <= 0) {

                    printf("Invalid quantity.\n");

                    break;

                }


                // Add to cart

                strcpy(cart_items[cart_count], inventory_items[i]);

                cart_prices[cart_count] = inventory_prices[i];

                cart_quantities[cart_count] = quantity;

                cart_count++;

                

                printf("Added %d %s(s) to cart.\n", quantity, inventory_items[i]);

                break;

            }

        }


        if (!found) {

            printf("Sorry, item '%s' is not in stock.\n", input_item);

        }


        if (cart_count >= MAX_CART) {

            printf("Cart is full!\n");

            break;

        }


        printf("Do you want to add more items? (y/n): ");

        scanf(" %c", &choice);

        printf("\n");


    } while ((choice == 'y' || choice == 'Y') && cart_count < MAX_CART);


    // Print Invoice

    if (cart_count > 0) {

        double total_bill = 0.0;

        

        printf("\n=========================================\n");

        printf("                INVOICE                  \n");

        printf("=========================================\n");

        printf("%-15s %-10s %-8s %-8s\n", "Item", "Price", "Qty", "Total");

        printf("-----------------------------------------\n");


        for (int i = 0; i < cart_count; i++) {

            double item_total = cart_prices[i] * cart_quantities[i];

            total_bill += item_total;

            

            // Capitalize first letter for invoice presentation

            cart_items[i][0] = toupper((unsigned char)cart_items[i][0]);

            

            printf("%-15s $%-9.2f %-8d $%-8.2f\n", 

                   cart_items[i], cart_prices[i], cart_quantities[i], item_total);

        }


        printf("-----------------------------------------\n");

        printf("%-35s $%-8.2f\n", "GRAND TOTAL:", total_bill);

        printf("=========================================\n");

        printf("        Thank you for shopping!          \n");

    } else {

        printf("No items purchased. Cart is empty.\n");

    }


    return 0;

}


No comments:

C program to print invoice

 #include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_ITEMS 5 #define MAX_NAME_LEN 20 #define MAX_CART 10...