Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do I use iterators instead of auto to find a value from a list? In the withdraw and deposit and findaccountbyid function. #include #include

how do I use iterators instead of auto to find a value from a list? In the withdraw and deposit and findaccountbyid function.

#include #include

using namespace std;

class Account { public: string name; double balance; Account(string n, double b) : name(n), balance(b) {} };

void displayAccountInfo(list accounts) { for (auto account : accounts) { cout << "Name: " << account.name << endl; cout << "Balance: " << account.balance << endl; } }

void addDeposit(list& accounts) { string name; double deposit; cout << "Enter the name of the account: "; cin >> name; cout << "Enter the deposit amount: "; cin >> deposit;

for (auto& account : accounts) { if (account.name == name) { account.balance += deposit; cout << "Deposit added successfully" << endl; return; } }

cout << "Account not found" << std::endl; }

void withdraw(list& accounts) { string name; double withdraw; cout << "Enter the name of the account: "; cin >> name; cout << "Enter the withdrawal amount: "; cin >> withdraw;

for (auto& account : accounts) { if (account.name == name) { if (account.balance < withdraw) { cout << "Insufficient funds" << endl; return; } else { account.balance -= withdraw; cout << "Withdrawal successful" << endl; return; } } }

cout << "Account not found" << endl; }

void addAccount(list& accounts) { string name; double balance; cout << "Enter the name of the account: "; cin >> name; cout << "Enter the initial balance: "; cin >> balance;

accounts.emplace_back(name, balance); cout << "Account created successfully" << endl; }

void findAccountById(list& accounts) { int id; cout << "Enter the ID of the account: "; cin >> id; int i = 0; for (auto& account : accounts) { if (i == id) { cout << "Account ID: " << id << " Name: " << account.name << " Balance: $" << account.balance << endl; return; } i++; } cout << "Account not found" << endl; }

int main() { list accounts;

while (true) { int choice; cout << "Main Menu:" << endl; cout << "0. Quit Program" << endl; cout << "1. Display account information" << endl; cout << "2. Add a deposit to an account" << endl; cout << "3. Withdraw from an account" << endl; cout << "4. Add new account" << endl; cout << "5. Find account by ID" << endl; cout << "Your choice: "; cin >> choice; if (choice == 0) { break; } else if (choice == 1) { if (accounts.empty()) { cout << "No accounts found" << endl; continue; } displayAccountInfo(accounts); } else if (choice == 2) { if (accounts.empty()) { cout << "No accounts found" << endl; continue; } addDeposit(accounts); } else if (choice == 3) { if (accounts.empty()) { cout << "No accounts found" << endl; continue; } withdraw(accounts); } else if (choice == 4) { addAccount(accounts); } else if(choice ==5){ if (accounts.empty()) { cout << "No accounts found" << endl; continue; } findAccountById(accounts); }else { cout << "Invalid choice. Please try again." << endl; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions