cplusplus
cplusplus

 

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:

  1. Ensure you have a C++ compiler installed (such as g++ or Visual Studio).
  2. Create a new C++ file (e.g., shopping_list.cpp) and copy the code into this file.
  3. Compile the program using the following command (for g++): g++ shopping_list.cpp -o shopping_list
  4. Run the compiled program: ./shopping_list
  5. Follow the on-screen instructions to interact with the shopping list application.
© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)