Project Overview
This interactive terminal application simulates an online shopping experience in C++. It protects access with a password, offers five product categories, tracks a running bill, and guides users through a menu-driven purchase flow. Developers can explore C++ control structures, I/O handling and menu design.
Key Features
- Password Gate
Prompts for a preset password before granting access to shopping menus. - Five Shopping Categories
• Grocery
• Laptops
• Vehicles
• Sunglasses
• Clothing - Running Bill
Updates and displays the total after each item selection. - Checkout & Exit
Allows users to finalize purchases and view the total bill before exiting.
Typical Use Cases
- Demo Program
Showcase console‐based menus, loops and conditional logic. - C++ Learning Exercise
Study menu implementation, simple data handling and incremental billing. - Interactive Prototype
Prototype a text‐based shopping workflow.
Installation & Execution
- Clone the repository
git clone https://github.com/Krishna-Gowami/cpp-Online_Shopping_project.git cd cpp-Online_Shopping_project
- Compile with g++
g++ shopping.cpp -o shopping
- Run the application
./shopping
Interaction Flow
- Enter the correct password at the prompt.
- View the main menu of five categories.
- Select a category to list its items and prices.
- Choose items and specify quantities.
- See your running total after each selection.
- Return to the main menu or proceed to checkout.
- On checkout, view the final bill and exit.
Getting Started & Usage
This section guides you through obtaining the code, compiling with g++/clang++, running the binary, entering the default password, navigating the on-screen menu, and completing an example purchase across two categories.
Prerequisites
- A C++17–compatible compiler (g++ or clang++)
- Git (for cloning the repository)
1. Clone the Repository
git clone https://github.com/Krishna-Gowami/cpp-Online_Shopping_project.git
cd cpp-Online_Shopping_project
2. Build the Project
Compile shopping.cpp
into an executable named shopping
:
g++ -std=c++17 shopping.cpp -o shopping
# or using clang++
clang++ -std=c++17 shopping.cpp -o shopping
3. Run the Application
./shopping
- When prompted, enter the default password:
admin
4. Navigate the On-Screen Menu
After successful login, the application displays the main menu:
Main Menu:
1. Grocery
2. Laptops
3. Vehicles
4. Sunglasses
5. Clothing
6. Exit
Select a category:
- Enter the number corresponding to a category.
- Within each category, the program lists items with an ID, name, and price.
- To purchase:
- Enter the Item ID.
- Enter the quantity.
- The system adds the cost (
price × quantity
) to your running total.
- After each purchase, the main menu reappears. Choose another category or select 6 to exit.
5. Example Session: Purchase from Two Categories
This example buys 2 × Rice (Grocery) and 1 × Dell XPS 13 (Laptops), then exits.
$ ./shopping
Enter password: admin
Main Menu:
1. Grocery
2. Laptops
3. Vehicles
4. Sunglasses
5. Clothing
6. Exit
Select a category: 1
Grocery Items:
ID Name Price
1 Rice $10
2 Wheat $8
3 Apple $2
Select item ID: 1
Enter quantity: 2
Added 2 x Rice to cart ($20)
Returning to Main Menu...
Select a category: 2
Laptops Items:
ID Name Price
1 Dell XPS 13 $1000
2 MacBook Pro $1500
3 HP Spectre $900
Select item ID: 1
Enter quantity: 1
Added 1 x Dell XPS 13 to cart ($1000)
Returning to Main Menu...
Select a category: 6
Exiting...
Your total bill: $1020
Thank you for shopping!
6. Rebuild After Changes
If you modify shopping.cpp
, recompile:
g++ -std=c++17 shopping.cpp -o shopping
You’re now ready to explore and extend the online shopping system. Enjoy!
Code Walkthrough
This section breaks down the key components of shopping.cpp
to help you locate and modify core logic.
1. main() Program Flow
The main()
function handles authentication, displays the main menu, dispatches category handlers, and prints the final bill.
#include "shopping.h" // contains prototypes and PASSWORD constant
int main() {
double totalBill = 0.0;
// 1. Password protection
if (!checkPassword()) {
return 0;
}
// 2. Main menu loop
char choice;
do {
cout << "\n=== Online Shopping ===\n"
<< "1. Grocery\n"
<< "2. Laptops\n"
<< "3. Vehicles\n"
<< "4. Sunglasses\n"
<< "5. Clothes\n"
<< "6. Checkout\n"
<< "Select option: ";
cin >> choice;
switch (choice) {
case '1': grocery(totalBill); break;
case '2': laptop(totalBill); break;
case '3': vehicle(totalBill); break;
case '4': sunglasses(totalBill); break;
case '5': clothes(totalBill); break;
case '6': /* exit loop */ break;
default: cout << "Invalid choice\n";
}
} while (choice != '6');
// 3. Final billing
cout << "\nYour total bill is: $" << totalBill << "\n";
return 0;
}
2. Password Check Routine
Validates a three-attempt password before granting access.
bool checkPassword() {
const int MAX_TRIES = 3;
string input;
for (int i = 0; i < MAX_TRIES; ++i) {
cout << "Enter password: ";
cin >> input;
if (input == PASSWORD) {
cout << "Access granted\n";
return true;
}
cout << "Incorrect (" << MAX_TRIES - i - 1 << " tries left)\n";
}
cout << "Access denied\n";
return false;
}
To change the required password, update the PASSWORD
constant in shopping.h
.
3. Category Menu Functions
Each category function displays a list of items, accepts user selection and quantity, then updates the shared bill.
grocery()
void grocery(double &bill) {
int choice, qty;
double price = 0.0;
cout << "\n-- Grocery Menu --\n"
<< "1. Apples $2.50/kg\n"
<< "2. Bread $1.20/loaf\n"
<< "3. Milk $0.99/liter\n"
<< "Select item: ";
cin >> choice;
cout << "Quantity: ";
cin >> qty;
switch (choice) {
case 1: price = 2.50; break;
case 2: price = 1.20; break;
case 3: price = 0.99; break;
default: cout << "Invalid item\n"; return;
}
bill += price * qty;
cout << "Added $" << price * qty << " to bill\n";
}
To update grocery prices, modify the price
assignments above.
laptop(), vehicle(), sunglasses(), clothes()
All other category functions follow the same pattern:
- Display numbered list with prices.
- Read
choice
andqty
. - Map
choice
to aprice
. - Update
bill += price * qty
.
Locate their definitions in shopping.cpp
to adjust items or add new ones.
4. Billing Accumulator
double totalBill
lives inmain()
.- All category handlers accept
double &bill
and dobill += ...
. - Final output occurs after the main loop.
To apply discounts or taxes globally, adjust totalBill
before printing:
totalBill *= 0.90; // apply 10% discount
5. Utility I/O Patterns
- Use
cout
for prompts and menus. - Use
cin
for direct integer/string input. - Minimal validation: each menu checks valid range via
switch
.
For robust input (e.g., non-numeric guard), wrap cin
in validation loops:
while (!(cin >> choice) || choice < '1' || choice > '6') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter a number between 1 and 6: ";
}
Adding New Categories
- Declare new prototype in
shopping.h
:void newCategory(double &bill);
- Implement
newCategory
inshopping.cpp
following existing patterns. - In
main()
, add a menu entry andcase
branch:
cout << "7. NewCategory\n";
// ...
case '7': newCategory(totalBill); break;
Changing Prices
- Prices are hardcoded in each category’s
switch
block. - Search for price literals in that function and update accordingly.
- For dynamic pricing, consider defining price arrays or constants at the top of
shopping.cpp
.