C++ Mini Projects: Expense Tracker
Hey guys I'm back with another cool project: Expense Tracker. The program helps user
to add expenses according to category, amount spent with all the dates in tabular form and the user can also view all the money spent . It will involve calculation of money spent and then breaking it down according to different categories. Then you can save and load all the data of expense tracking for persistence. So lets go build this project!
Skills used
Input, output handling...
Adding expenses
Input from user in category, eg. Food, rental or conveyance.
Amount spent...
Date... entered in format of yyyy-mm-dd.
The code to build the desired project
Code...#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <map>
using namespace std;
// Expense structure
struct Expense {
string category;
double amount;
string date;
Expense(string c, double a, string d) : category(c), amount(a), date(d) {}
};
// Expense Tracker Class
class ExpenseTracker {
private:
vector<Expense> expenses;
public:
// Add a new expense
void addExpense() {
string category, date;
double amount;
cout << "Enter category (e.g., Food, Transport, etc.): ";
cin.ignore();
getline(cin, category);
cout << "Enter amount: ";
cin >> amount;
cout << "Enter date (YYYY-MM-DD): ";
cin.ignore();
getline(cin, date);
expenses.emplace_back(category, amount, date);
cout << "Expense added successfully!\n";
}
// View all recorded expenses
void viewExpenses() const {
if (expenses.empty()) {
cout << "No expenses recorded yet.\n";
return;
}
cout << setw(15) << "Category" << setw(10) << "Amount" << setw(15) << "Date\n";
cout << string(40, '-') << endl;
for (const auto &expense : expenses) {
cout << setw(15) << expense.category << setw(10) << expense.amount << setw(15) << expense.date << endl;
}
}
// Calculate total and category-wise expenses
void calculateExpenses() const {
if (expenses.empty()) {
cout << "No expenses recorded yet.\n";
return;
}
double total = 0;
map<string, double> categoryTotals;
for (const auto &expense : expenses) {
total += expense.amount;
categoryTotals[expense.category] += expense.amount;
}
cout << "Total Expenses: " << total << "\n\n";
cout << "Category-wise Breakdown:\n";
for (const auto &entry : categoryTotals) {
cout << setw(15) << entry.first << ": " << entry.second << endl;
}
}
// Save expenses to a file
void saveToFile() const {
ofstream outFile("expenses.txt");
if (!outFile) {
cout << "Error saving data to file.\n";
return;
}
for (const auto &expense : expenses) {
outFile << expense.category << "\n" << expense.amount << "\n" << expense.date << "\n";
}
outFile.close();
cout << "Expenses saved successfully!\n";
}
// Load expenses from a file
void loadFromFile() {
ifstream inFile("expenses.txt");
if (!inFile) {
cout << "No saved data found.\n";
return;
}
expenses.clear();
string category, date;
double amount;
while (getline(inFile, category) && (inFile >> amount) && inFile.ignore() && getline(inFile, date)) {
expenses.emplace_back(category, amount, date);
}
inFile.close();
cout << "Expenses loaded successfully!\n";
}
};
// Main function
int main() {
ExpenseTracker tracker;
int choice;
// Load data from file
tracker.loadFromFile();
do {
cout << "\nExpense Tracker\n";
cout << "1. Add Expense\n";
cout << "2. View Expenses\n";
cout << "3. Calculate Expenses\n";
cout << "4. Save Expenses\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
tracker.addExpense();
break;
case 2:
tracker.viewExpenses();
break;
case 3:
tracker.calculateExpenses();
break;
case 4:
tracker.saveToFile();
break;
case 5:
cout << "Exiting the program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Explanation
All expenses can be viewed in tabular form Category Amount Date
-----------------------------------------
Food 200.50 2024-12-01
Transport 50.00 2024-12-02
Calculation of expenses
Total ...
Sum of the expenses
Breakdown according to categories
Total Expenses: 250.50
Category-wise Breakdown:
Food: 200.50
Transport: 50.00
Saving and loading the expenses. Save is for storing the expenses data in the file
Expenses.txt.
Then loading the data that is saved automatically
Sample running of the program
Main menu
Expense Tracker
1. Add Expense
2. View Expenses
3. Calculate Expenses
4. Save Expenses
5. Exit
Enter your choice:
Adding new expense
Enter category (e.g., Food, Transport, etc.): Food
Enter amount: 200.50
Enter date (YYYY-MM-DD): 2024-12-01
Expense added successfully!
View the expense
Category Amount Date
-----------------------------------------
Food 200.50 2024-12-01
Calculate the expense
Total Expenses: 200.50
Category-wise Breakdown:
Food: 200.50
That's it, we've successfully built this handy tool using our own skills in C++.
See you next time with another cool project!
Comments
Post a Comment