The Simple POS System in C
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.
#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.
Read Also:
- Become a Data Analyst at Google
- Mastering Data Analysis skill and tips
- 16 Best Free data analyst Course with Certificate
Code Breakdown
1. Structure Definition (struct Item
)
struct Item {
char name[50];
float price;
int quantity;
};
This struct
represents an item in the POS system. It has:
name
: A string for the item name.price
: A float for the price per unit of the item.quantity
: An integer for the number of units chosen.
2. Predefined Items
Three arrays of struct Item
represent categories of items available in the POS system.
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 items, their 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 adding to the
total
. - Applies tax by adding
total * taxRate / 100
to thetotal
.
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 total before tax and the 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 price.
6. main
Function
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;
}
Step-by-Step Workflow
Category Selection:
The user is prompted to choose a category (drinks, fruits, or groceries).
Display Items:
The items in the chosen category are displayed, showing their names and prices.
Item Selection:
The user specifies how many items they wish to purchase and the quantity of each.
Tax Input:
The user enters the tax rate (as a percentage).
Receipt Generation:
- The
calculateTotal
function computes the total cost including tax. - The
printReceipt
function displays a detailed receipt.
Example Run:
Choose a category: Drinks Fruits Groceries Enter your choice: Drinks Items available: Coke - $1.50 Pepsi - $1.40 Water - $1.00 Juice - $2.50 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
This breakdown provides a detailed explanation of how the code functions and the step-by-step logic it follows to execute the POS system.