🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

This is a very handy app used to split expenses between people. When multiple individuals contribute money for a shared expense, this C++ expense splitter helps determine each person's fair share. Isn't that interesting and useful? Let's dive in and build one!
setprecision
and fixed
to format output.#include <iostream>
#include <vector>
#include <string>
#include <iomanip> // for fixed and setprecision
using namespace std;
struct Person {
string name;
double amountPaid;
};
void calculateExpenses(const vector<Person>& people) {
double total = 0.0;
int numPeople = people.size();
// Calculate total expense
for (const auto& person : people) {
total += person.amountPaid;
}
double average = total / numPeople;
cout << "\nTotal expense: $" << fixed << setprecision(2) << total << endl;
cout << "Each person should contribute: $" << fixed << setprecision(2) << average << endl;
// Calculate who owes or gets back
cout << "\nSummary of contributions:\n";
for (const auto& person : people) {
double balance = person.amountPaid - average;
if (balance > 0) {
cout << person.name << " should get back $" << fixed << setprecision(2) << balance << endl;
} else if (balance < 0) {
cout << person.name << " owes $" << fixed << setprecision(2) << -balance << endl;
} else {
cout << person.name << " is settled.\n";
}
}
}
int main() {
vector<Person> people;
int numPeople;
cout << "Welcome to the Expense Splitter!" << endl;
cout << "Enter the number of people: ";
cin >> numPeople;
// Input data for each person
for (int i = 0; i < numPeople; ++i) {
Person person;
cout << "\nEnter name of person " << i + 1 << ": ";
cin >> person.name;
cout << "Enter the amount paid by " << person.name << ": $";
cin >> person.amountPaid;
people.push_back(person);
}
// Calculate and display expense summary
calculateExpenses(people);
return 0;
}
struct Person {
string name;
double amountPaid;
};
name
: Stores the participant’s name.amountPaid
: Stores the amount paid by the person.vector<Person> people;
int numPeople;
cout << "Enter the number of people: ";
cin >> numPeople;
vector<Person>
to dynamically store participant details.double total = 0.0;
for (const auto& person : people) {
total += person.amountPaid;
}
double average = total / numPeople;
total
: Sums all contributions.average
: Divides the total by the number of people to determine equal shares.for (const auto& person : people) {
double balance = person.amountPaid - average;
if (balance > 0) {
cout << person.name << " should get back $" << fixed << setprecision(2) << balance << endl;
} else if (balance < 0) {
cout << person.name << " owes $" << fixed << setprecision(2) << -balance << endl;
} else {
cout << person.name << " is settled.\n";
}
}
balance
: Determines the amount each person owes or is owed.Input:
Enter the number of people: 3
Enter name of person 1: Alice
Enter the amount paid by Alice: $100
Enter name of person 2: Bob
Enter the amount paid by Bob: $50
Enter name of person 3: Charlie
Enter the amount paid by Charlie: $150
Output:
Total expense: $300.00
Each person should contribute: $100.00
Summary of contributions:
Alice is settled.
Bob owes $50.00
Charlie should get back $50.00
A group of friends at a café place a shared order. After the meal, they receive a $200 bill, and each pays a different amount based on cash in hand. Instead of manually calculating who owes whom, they can use the expense splitter to determine fair contributions.
In workplaces, teams often pool money for office supplies, gifts, or snacks. Each colleague contributes differently, and the expense splitter ensures fair contributions while preventing misunderstandings.
When planning group trips, different people may book flights, accommodations, or food. The expense splitter fairly distributes costs to ensure no one overpays.
A group of five college students take turns paying for gas, food, and lodging on a trip. The total expenses amount to $750, with each contributing differently. Using the expense splitter, they calculate who needs to pay whom for a fair distribution.
Advantages:
Employees collect money to buy a wedding gift for a coworker. Some contribute more, while others contribute less. Instead of confusion, the expense splitter calculates each person's fair share and ensures smooth financial cooperation.
Advantages:
Floating point calculations can lead to rounding issues. The program uses setprecision(2)
and fixed
to consistently display amounts to two decimal places, ensuring accuracy.
For large groups (50+ people), the program efficiently processes data using vectors and optimized algorithms for better performance.
A future enhancement could involve integrating a payment API, allowing users to transfer funds directly within the app.
This project is a fantastic starting point for more advanced financial management applications. Hope you enjoyed it! Stay tuned for another exciting project soon!
Comments
Post a Comment