Introduction
A shopping list application helps users manage their shopping needs by allowing them to add and remove items. It keeps track of the items and their quantities. In this program, we will create a simple shopping list application in C++ that lets the user add items to the list, remove them, and view the current shopping list.
Objective
The objective of this program is to demonstrate basic C++ concepts like arrays, loops, and user input/output. The application allows users to:
- Add items to the shopping list
- Remove items from the shopping list
- Display the current shopping list
- Exit the application gracefully
Code
#include #include #include using namespace std; // Function to display the menu void displayMenu() { cout << "\n--- Shopping List Application ---\n"; cout << "1. Add an item to the list\n"; cout << "2. Remove an item from the list\n"; cout << "3. Display shopping list\n"; cout << "4. Exit\n"; cout << "Choose an option: "; } // Function to add an item to the shopping list void addItem(vector& shoppingList) { string item; cout << "Enter item name: "; cin.ignore(); // Ignore leftover newline from previous input getline(cin, item); shoppingList.push_back(item); cout << item << " has been added to your shopping list.\n"; } // Function to remove an item from the shopping list void removeItem(vector& shoppingList) { string item; cout << "Enter item name to remove: "; cin.ignore(); // Ignore leftover newline from previous input getline(cin, item); auto it = find(shoppingList.begin(), shoppingList.end(), item); if (it != shoppingList.end()) { shoppingList.erase(it); cout << item << " has been removed from your shopping list.\n"; } else { cout << item << " not found in the shopping list.\n"; } } // Function to display the shopping list void displayList(const vector& shoppingList) { if (shoppingList.empty()) { cout << "Your shopping list is empty.\n"; } else { cout << "Current Shopping List:\n"; for (const auto& item : shoppingList) { cout << "- " << item << "\n"; } } } int main() { vector shoppingList; int choice; while (true) { displayMenu(); cin >> choice; switch (choice) { case 1: addItem(shoppingList); break; case 2: removeItem(shoppingList); break; case 3: displayList(shoppingList); break; case 4: cout << "Exiting the shopping list application.\n"; return 0; default: cout << "Invalid choice. Please try again.\n"; } } return 0; }
Explanation of Program Structure
This program defines a shopping list application with several functions to manage the list:
- displayMenu: Displays the options available to the user, such as adding or removing items, viewing the list, or exiting.
- addItem: Prompts the user to input an item, then adds it to the shopping list using a vector.
- removeItem: Allows the user to remove an item from the shopping list if it exists.
- displayList: Displays the entire current shopping list. If the list is empty, it informs the user accordingly.
The program uses a vector to store the shopping list. Vectors are dynamic arrays that allow flexible resizing as items are added or removed. The program runs in a loop, continuously displaying the menu until the user chooses to exit.
How to Run the Program
To run the shopping list application, follow these steps:
- Ensure you have a C++ compiler installed (such as g++ or Visual Studio).
- Create a new C++ file (e.g.,
shopping_list.cpp
) and copy the code into this file. - Compile the program using the following command (for g++):
g++ shopping_list.cpp -o shopping_list
- Run the compiled program:
./shopping_list
- Follow the on-screen instructions to interact with the shopping list application.