I am attempting to make a program that stores records of user accounts, including their name, balance, account type etc. Such that the user can enter a line of text denoting the desired action (create an account, withdraw etc), the account name, and account type/amount.
example of input: c josh y
c creates an account
josh is the name
y is a credit account
I am having trouble finding a way of iterating through the listAccounts vector in change_balance() such that the user can find a particular account by its name in vector listAccounts, and change the balance of that particular object in the vector.
void foo( string name )
{
auto it = std::find_if( accounts.begin(), accounts.end(),
[&name](const Account& A){ return A.account_name == name; } );
if ( accounts.end() != it )
{
// use it
}
}
void Account::init_account(string name, char type) {
Account account;
if (type == 'y') { //setting the account type (credit/basic)
account.account_type = 1;
} elseif (type == 'n') {
account.account_type = 0;
}
account.account_name = name;
account.account_balance = 0;
listAccounts.push_back(account);
}
void Account::change_balance(string name, int balance) {
auto iter = listAccounts.begin();
for ( ; iter != listAccounts.end(); iter++) {
if ((*iter).account_name == name) {
(*iter).account_balance = balance;
cout << (*iter).account_balance;
}
if (((*iter).account_type == 0) && ((*iter).account_balance < 0)) {
cout << "account cannot hold negative balance""\n";
(*iter).account_balance = (((*iter).account_balance) - (balance));
cout << (*iter).account_balance;
}
}
}
void Account::remove_account(string name) {
auto iter = listAccounts.begin();
for ( ; iter != listAccounts.end(); iter++) {
if ((*iter).account_name == name) {
listAccounts.erase(iter);
break;
}
}
}
I also want to be able to iterate through the entire array in change_balance() to check if an element with name exists in the first place, but I can't think for the life of me how to search through the array to check if the name exists, if it doesn't, cout "does not exist".