Simple POS System in C

Simple POS System in C (Point of Sale)

The Simple POS System in C is a command-line program that serves as an effective point-of-sale solution. It allows users to select items from predefined categories like drinks, fruits, and groceries, specify quantities, and receive an itemized receipt with tax calculations. This program is perfect for learning C programming concepts such as structures, arrays, and basic user input handling.

Explore More C & Billing Projects: If you are interested in commercial software logic, check out our guide on creating a Simple Billing Invoice System and our beginner-friendly tutorial on building a Contact Management System using C

C Source Code

#include <stdio.h>

// Structure to represent an item in the POS system
struct Item {
    char name[50];
    float price;
    int quantity;
};

// Predefined items
struct Item drinks[] = {
    {"Coke", 1.50, 0},
    {"Pepsi", 1.40, 0},
    {"Water", 1.00, 0},
    {"Juice", 2.50, 0},
    {"Energy Drink", 3.00, 0}
};

struct Item fruits[] = {
    {"Apple", 1.20, 0},
    {"Banana", 0.50, 0},
    {"Orange", 0.80, 0},
    {"Grapes", 2.00, 0},
    {"Mango", 1.50, 0}
};

struct Item groceries[] = {
    {"Bread", 1.50, 0},
    {"Milk", 2.00, 0},
    {"Eggs", 3.00, 0},
    {"Cheese", 4.00, 0},
    {"Butter", 2.50, 0}
};

// Function to calculate the total cost and apply tax
float calculateTotal(struct Item items[], int numItems, float taxRate) {
    float total = 0.0;
    for (int i = 0; i < numItems; i++) {
        total += items[i].price * items[i].quantity;
    }
    return total + (total * taxRate / 100);
}

// Function to print the receipt
void printReceipt(struct Item items[], int numItems, float total, float taxRate) {
    printf("\nReceipt:\n");
    printf("---------------------------------\n");
    for (int i = 0; i < numItems; i++) {
        if (items[i].quantity > 0) {
            printf("%s (x%d): $%.2f\n", items[i].name, items[i].quantity, items[i].price * items[i].quantity);
        }
    }
    printf("---------------------------------\n");
    printf("Total before tax: $%.2f\n", total / (1 + taxRate / 100));
    printf("Tax (%.2f%%): $%.2f\n", taxRate, total - (total / (1 + taxRate / 100)));
    printf("Total amount due: $%.2f\n", total);
}

void displayItems(struct Item items[], int numItems) {
    for (int i = 0; i < numItems; i++) {
        printf("%d. %s - $%.2f\n", i + 1, items[i].name, items[i].price);
    }
}

int main() {
    int categoryChoice, itemChoice, numItems = 0;
    float taxRate;

    // Display categories
    printf("Choose a category:\n");
    printf("1. Drinks\n");
    printf("2. Fruits\n");
    printf("3. Groceries\n");
    printf("Enter your choice: ");
    scanf("%d", &categoryChoice);

    struct Item *selectedCategory;
    int selectedSize;

    switch (categoryChoice) {
        case 1:
            selectedCategory = drinks;
            selectedSize = sizeof(drinks) / sizeof(drinks[0]);
            break;
        case 2:
            selectedCategory = fruits;
            selectedSize = sizeof(fruits) / sizeof(fruits[0]);
            break;
        case 3:
            selectedCategory = groceries;
            selectedSize = sizeof(groceries) / sizeof(groceries[0]);
            break;
        default:
            printf("Invalid category choice. Exiting.\n");
            return 1;
    }

    // Display items in the selected category
    printf("\nItems available:\n");
    displayItems(selectedCategory, selectedSize);

    // Get item choices and quantities from the user
    printf("\nEnter the number of items to purchase: ");
    scanf("%d", &numItems);

    struct Item items[numItems];
    for (int i = 0; i < numItems; i++) {
        printf("\nEnter details for item %d:\n", i + 1);
        printf("Item number: ");
        scanf("%d", &itemChoice);
        if (itemChoice < 1 || itemChoice > selectedSize) {
            printf("Invalid item choice. Please try again.\n");
            i--;
            continue;
        }
        items[i] = selectedCategory[itemChoice - 1];
        printf("Quantity: ");
        scanf("%d", &items[i].quantity);
    }

    // Get the tax rate
    printf("\nEnter the tax rate (in %%): ");
    scanf("%f", &taxRate);

    // Calculate the total cost including tax
    float total = calculateTotal(items, numItems, taxRate);

    // Print the receipt
    printReceipt(items, numItems, total, taxRate);

    return 0;
}

Overview

This code is a simple Point-of-Sale (POS) system written in C. It allows users to:

  • Choose from predefined categories of items (drinks, fruits, groceries).
  • Select specific items from the chosen category.
  • Specify the quantity of each item.
  • Calculate and display a detailed receipt with tax information.

Code Breakdown

1. Structure Definition (struct Item)

struct Item {
    char name[50];
    float price;
    int quantity;
};

This C struct represents an item in the POS system. It contains three fields:

  • name: A string array storing the item name.
  • price: A floating-point number representing unit price.
  • quantity: An integer keeping track of selected units.

2. Predefined Items

Three arrays of struct Item represent the distinct inventories available:

Drinks Array:

struct Item drinks[] = {
    {"Coke", 1.50, 0},
    {"Pepsi", 1.40, 0},
    {"Water", 1.00, 0},
    {"Juice", 2.50, 0},
    {"Energy Drink", 3.00, 0}
};

Fruits Array:

struct Item fruits[] = {
    {"Apple", 1.20, 0},
    {"Banana", 0.50, 0},
    {"Orange", 0.80, 0},
    {"Grapes", 2.00, 0},
    {"Mango", 1.50, 0}
};

Groceries Array:

struct Item groceries[] = {
    {"Bread", 1.50, 0},
    {"Milk", 2.00, 0},
    {"Eggs", 3.00, 0},
    {"Cheese", 4.00, 0},
    {"Butter", 2.50, 0}
};

Each array is initialized with item names, prices, and quantities set to 0 initially.

3. calculateTotal Function

float calculateTotal(struct Item items[], int numItems, float taxRate) {
    float total = 0.0;
    for (int i = 0; i < numItems; i++) {
        total += items[i].price * items[i].quantity;
    }
    return total + (total * taxRate / 100);
}

Purpose: Calculates the total cost including tax.

Logic:

  • Loops through all items, multiplying the price by the quantity and accumulating the subtotal.
  • Applies tax by adding total * taxRate / 100 to the base subtotal.

4. printReceipt Function

void printReceipt(struct Item items[], int numItems, float total, float taxRate) {
    printf("\nReceipt:\n");
    printf("---------------------------------\n");
    for (int i = 0; i < numItems; i++) {
        if (items[i].quantity > 0) {
            printf("%s (x%d): $%.2f\n", items[i].name, items[i].quantity, items[i].price * items[i].quantity);
        }
    }
    printf("---------------------------------\n");
    printf("Total before tax: $%.2f\n", total / (1 + taxRate / 100));
    printf("Tax (%.2f%%): $%.2f\n", taxRate, total - (total / (1 + taxRate / 100)));
    printf("Total amount due: $%.2f\n", total);
}

Purpose: Prints a formatted receipt.

Logic:

  • Prints item details (name, quantity, and cost per item) if the quantity is greater than 0.
  • Displays the subtotal before tax and the calculated tax amount.
  • Shows the final amount due.

5. displayItems Function

void displayItems(struct Item items[], int numItems) {
    for (int i = 0; i < numItems; i++) {
        printf("%d. %s - $%.2f\n", i + 1, items[i].name, items[i].price);
    }
}

Purpose: Displays the available items in a category.

Logic: Loops through the array and prints each item number, name, and unit price.

6. main Function

Controls user flow, prompts category options via a switch-case structure, collects item index choices, validates inputs, and coordinates receipt printing.

Step-by-Step Workflow

Step Phase Description
1 Category Selection The user is prompted to choose a category (drinks, fruits, or groceries).
2 Display Items The items in the chosen category are displayed, showing names and unit prices.
3 Item Selection The user specifies how many items they wish to purchase and the quantity of each.
4 Tax Input The user enters the applicable tax rate as a percentage.
5 Receipt Generation calculateTotal computes the total cost including tax, and printReceipt prints the itemized summary.

Example Run

Choose a category:
1. Drinks
2. Fruits
3. Groceries
Enter your choice: 1

Items available:
1. Coke - $1.50
2. Pepsi - $1.40
3. Water - $1.00
4. Juice - $2.50
5. Energy Drink - $3.00

Enter the number of items to purchase: 2

Enter details for item 1:
Item number: 1
Quantity: 3

Enter details for item 2:
Item number: 4
Quantity: 1

Enter the tax rate (in %): 5

Receipt:
---------------------------------
Coke (x3): $4.50
Juice (x1): $2.50
---------------------------------
Total before tax: $7.00
Tax (5.00%): $0.35
Total amount due: $7.35

Conclusion

This breakdown provides a detailed explanation of how the code functions and the step-by-step logic it follows to execute the POS system. It offers a solid starting framework that can later be expanded with file storage, database support, or dynamic linked lists.

Post a Comment

Previous Post Next Post