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
- Ensure you have a C++ compiler installed (like GCC or Clang).
- Create a new C++ file and paste the provided code into it.
- Save the file with a .cpp extension (e.g., expense_tracker.cpp).
- Open the terminal/command prompt and navigate to the folder where your file is saved.
- Compile the program using a command like
g++ expense_tracker.cpp -o expense_tracker
. - Run the program with the command
./expense_tracker
(on Unix systems) orexpense_tracker.exe
(on Windows).