Welcome to the Personal Expense Tracker. This program helps you track and categorize your daily expenses. You can monitor your spending habits and organize expenses based on different categories. It’s an excellent tool to stay on top of your finances!

Objective

The objective of this C++ Expense Tracker is to create a simple console-based application that allows users to log their expenses, categorize them, and display the total for each category. The application is designed to be easy to use and can be expanded for more complex financial tracking needs.

Code

#include 
#include 
#include 

using namespace std;

struct Expense {
    string description;
    double amount;
    string category;
};

class ExpenseTracker {
private:
    vector expenses;

public:
    void addExpense() {
        Expense newExpense;
        cout << "Enter description of the expense: ";
        cin.ignore();  // to ignore the leftover newline character
        getline(cin, newExpense.description);
        cout << "Enter the amount: "; cin >> newExpense.amount;
        cout << "Enter the category: ";
        cin.ignore();  // to ignore the leftover newline character
        getline(cin, newExpense.category);

        expenses.push_back(newExpense);
        cout << "Expense added successfully!" << endl;
    }

    void displayExpenses() {
        cout << "\nExpense List:" << endl;
        for (const auto& expense : expenses) {
            cout << "Description: " << expense.description 
                 << ", Amount: $" << expense.amount 
                 << ", Category: " << expense.category << endl;
        }
    }

    void displayCategorySummary() {
        cout << "\nCategory Summary:" << endl;
        double totalAmount = 0;
        string category;
        cout << "Enter the category for summary: ";
        cin.ignore();
        getline(cin, category);

        for (const auto& expense : expenses) {
            if (expense.category == category) {
                totalAmount += expense.amount;
            }
        }

        cout << "Total amount spent in category '" << category << "': $" << totalAmount << endl;
    }

    void showMenu() {
        int choice;
        do {
            cout << "\n1. Add Expense\n";
            cout << "2. Display All Expenses\n";
            cout << "3. Display Expense Summary by Category\n";
            cout << "4. Exit\n";
            cout << "Enter your choice: "; cin >> choice;

            switch (choice) {
                case 1:
                    addExpense();
                    break;
                case 2:
                    displayExpenses();
                    break;
                case 3:
                    displayCategorySummary();
                    break;
                case 4:
                    cout << "Exiting the program..." << endl;
                    break;
                default:
                    cout << "Invalid choice. Please try again." << endl;
            }
        } while (choice != 4);
    }
};

int main() {
    ExpenseTracker tracker;
    tracker.showMenu();
    return 0;
}

Explanation of the Program

This C++ Expense Tracker program is built using a simple console-based interface where users can perform the following operations:

  • Adding an Expense: Users can add a new expense by entering a description, amount, and category.
  • Displaying All Expenses: The program displays all logged expenses, showing the description, amount, and category for each.
  • Category Summary: Users can enter a category and see the total amount spent in that category.

Program Structure

The program is divided into a Expense structure, which stores the description, amount, and category of each expense. The ExpenseTracker class contains methods for adding expenses, displaying expenses, and generating category summaries. The showMenu method provides an interactive menu for the user to choose what action to take. The main function initializes the program and calls the showMenu function.

How to Run the Program

  1. Ensure you have a C++ compiler installed (like GCC or Clang).
  2. Create a new C++ file and paste the provided code into it.
  3. Save the file with a .cpp extension (e.g., expense_tracker.cpp).
  4. Open the terminal/command prompt and navigate to the folder where your file is saved.
  5. Compile the program using a command like g++ expense_tracker.cpp -o expense_tracker.
  6. Run the program with the command ./expense_tracker (on Unix systems) or expense_tracker.exe (on Windows).
© 2025 Learn Programming. All Rights Reserved.

 

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 :)