Full Code Explanation
Header Files
#include <iostream> // For cout, cin (input/output)
#include <iomanip> // For setw, setprecision (formatting)
#include <string> // For using string objects
#include <fstream> // For file operations (saving receipt)
#include <ctime> // For getting current date and time
#include <limits> // For clearing input buffer
#include <cctype> // For tolower(), isdigit()
#include <cstdlib> // For system("cls") and atoi()
#include <sstream> // For ostringstream (building receipt text)
These are libraries we need for:
o Input/output
o String handling
o Date/time functions
o File saving
o Screen clearing
o Formatting
o Converting strings to numbers.
Global Constants and Variables
const int SIZE = 5; // Total number of bundles
string bundleNames[SIZE] = {"2.0GB", "5.0GB", "10.0GB", "22.0GB", "50.0GB"};
double bundleSizes[SIZE] = {2.0, 5.0, 10.0, 22.0, 50.0};
double bundlePrices[SIZE] = {1200, 2500, 4500, 8500, 15000};
int selected[SIZE] = {0}; // Stores how many times each bundle is selected
These define:
o Bundle names (displayed to user)
o Their sizes in GB
o Prices in Naira (₦)
o selected[] keeps track of user purchases.
Utility Function: Clear Screen
void clearScreen() {
system("cls");
}
Clears the console screen.
Works in Windows. For Linux/Mac use system("clear").
🔹 Utility Function: Print Header
void printHeader(const string &title) {
cout << "\
n==============================================================" <<
endl;
cout << " \033[1;32m" << title << "\033[0m" << endl;
cout <<
"==============================================================\n"
<< endl;
}
Prints a bold green header with title.
Uses ANSI escape codes \033[1;32m for color.
Utility Function: Print Bundles
void printBundles() {
cout << "\033[1;33mAvailable Glo Bundles:\033[0m\n";
cout << left << setw(8) << "No"
<< "| " << setw(12) << "Bundle"
<< "| " << setw(10) << "Size(GB)"
<< "| " << setw(12) << "Price(N)" << "\n";
cout << string(48, '-') << "\n";
for (int i = 0; i < SIZE; i++) {
cout << left << setw(8) << i + 1
<< "| " << setw(12) << bundleNames[i]
<< "| " << setw(10) << fixed << setprecision(2) << bundleSizes[i]
<< "| ₦" << setw(10) << fixed << setprecision(2) << bundlePrices[i] << "\n";
}
cout << string(48, '-') << "\n";
}
Displays the data bundles table:
o Columns: No, Bundle, Size, Price
o Uses setw to align columns.
o fixed + setprecision(2) keeps prices like 1200.00.
Colors added for better UX.
Utility Function: Save Receipt
void saveReceipt(const string &receiptText) {
ofstream file("Glo_Receipt.txt");
if (file.is_open()) {
file << receiptText;
[Link]();
cout << "\n\033[35mReceipt saved as 'Glo_Receipt.txt'\033[0m\n";
} else {
cout << "\033[31mFailed to save receipt.\033[0m\n";
}
}
Saves the receipt to a text file.
Uses ofstream for file output.
Main Program
int main() {
char again;
do {
Starts the program loop for multiple transactions.
Step 1: Initialize
clearScreen();
double totalGB = 0, totalPrice = 0;
for (int i = 0; i < SIZE; i++) selected[i] = 0;
printHeader("Welcome to Glo Data Subscription");
printBundles();
Clears screen, resets totals, prints header and bundles.
Step 2: User Purchases Bundles
int choice;
while (true) {
cout << "\nEnter bundle number to purchase (C to checkout): ";
string input;
cin >> input;
if (tolower(input[0]) == 'c') break; // Checkout
if (isdigit(input[0])) {
choice = atoi(input.c_str());
} else {
cout << "\033[31mInvalid input. Enter a number or C.\033[0m\n";
continue;
}
if (choice < 1 || choice > SIZE) {
cout << "\033[31mInvalid option. Try again.\033[0m\n";
continue;
}
int idx = choice - 1;
selected[idx]++;
totalGB += bundleSizes[idx];
totalPrice += bundlePrices[idx];
cout << "\033[32mAdded " << bundleSizes[idx] << " GB for ₦" << bundlePrices[idx]
<< "\033[0m\n";
}
Lets user select bundles:
o C to checkout.
o Validates input.
o Updates selected[], totalGB, totalPrice.
atoi() converts input safely.
Step 3: Checkout
clearScreen();
printHeader("Glo Checkout");
string customerName, phoneNumber;
[Link](numeric_limits<streamsize>::max(), '\n');
cout << "\033[36mEnter your name: \033[0m";
getline(cin, customerName);
cout << "\033[36mEnter your phone number: \033[0m";
getline(cin, phoneNumber);
Gets user details for the receipt.
Step 4: Build Receipt
time_t now = time(0);
tm *ltm = localtime(&now);
char dateTime[20];
strftime(dateTime, sizeof(dateTime), "%Y-%m-%d %H:%M", ltm);
ostringstream receipt;
receipt << "========================= GLO RECEIPT
=========================\n";
receipt << "Customer Name : " << customerName << "\n";
receipt << "Phone Number : " << phoneNumber << "\n";
receipt << "Date : " << dateTime << "\n";
receipt <<
"================================================================\
n";
receipt << left << setw(15) << "Bundle"
<< setw(10) << "Qty"
<< setw(15) << "Subtotal(N)\n";
receipt << string(40, '-') << "\n";
Adds receipt header, user details, and table headers.
Step 5: List Purchases
for (int i = 0; i < SIZE; i++) {
if (selected[i] > 0) {
double subtotal = bundlePrices[i] * selected[i];
cout << left << setw(15) << bundleNames[i]
<< setw(10) << selected[i]
<< "₦" << setw(15) << fixed << setprecision(2) << subtotal << "\n";
receipt << left << setw(15) << bundleNames[i]
<< setw(10) << selected[i]
<< "₦" << fixed << setprecision(2) << subtotal << "\n";
}
}
Shows each bundle purchased and their subtotal.
Step 6: Show Totals
double commission = totalPrice * 0.05;
double grandTotal = totalPrice + commission;
cout << string(40, '-') << "\n";
cout << "Total Data : " << totalGB << " GB\n";
cout << "Base Total : ₦" << fixed << setprecision(2) << totalPrice << "\n";
cout << "Commission (5%) : ₦" << fixed << setprecision(2) << commission << "\n";
cout << "Grand Total : ₦" << fixed << setprecision(2) << grandTotal << "\n";
Calculates and displays:
o Base total
o 5% commission
o Grand total
Step 7: Save Receipt
saveReceipt([Link]());
Calls the saveReceipt() function to save receipt as text file.
Step 8: Repeat or Exit
cout << "\n\033[36mWould you like to perform another transaction? (y/n): \033[0m";
cin >> again;
} while (tolower(again) == 'y');
cout << "\n\033[1;32mThank you for using Glo Data Subscription!\033[0m\n\n";
return 0;
}
Lets user repeat or exit.
Shows goodbye message in green.