I've been trying to figure this out for weeks now. My program will take in one user's information and display it using that display function. However, when I create another user, the previous user is overwritten. I'm aware I need to implement an array but I am at a complete loss as to where and how to do so.
Source.cpp
#include <iostream>
#include <string>
#include "BankAccount.h"
usingnamespace std;
int Menu()
{
int i;
cout << ("\n\n MENU \n\n");
cout << ("1. Create An Account \n");
cout << ("2. Display An Account \n");
cout << ("3. Withdraw Funds Into An Account \n");
cout << ("4. Deposit Funds From An Account \n");
cout << ("5. Take Out A Loan \n");
cout << ("6. Calculate Interest \n");
cout << ("7. Take Out An Overdraft \n");
cout << ("8. Exit \n\n");
cout << ("Enter Your Choice Here: ");
cin >> i;
cin.clear();
cin.ignore(INT_MAX, '\n');
return i;
}
int main()
{
BankAccount ca; //Create Account
/*BankAccount da; //Display Account
BankAccount w; //Withdraw Funds
BankAccount d; //Deposit Funds
BankAccount l; //Take Out Loan
BankAccount i; //Interest
BankAccount o; //Overdraft*/
int end = 0;
while (end != 1)
{
int op;
system("CLS");
op = Menu();
switch (op)
{
case 1:
ca.CreateAccount(); //Loads the CreateAccount function
break;
case 2:
ca.DisplayAccount(); //Loads the DisplayAccount function
break;
case 3:
ca.Withdraw(); //Loads the Withdraw function
break;
case 4:
ca.Deposit(); //Loads the Deposit function
break;
case 5:
ca.Loan(); //Loads the Loan Function
break;
case 6:
ca.Interest(); //Loads the Interest Function
break;
case 7:
ca.Overdraft(); //Loads the Overdraft Function
break;
case 8: // Exits The Program
end = 1;
break;
default:
cout << ("Please Enter a Valid Option.\n\n");
system("PAUSE");
break;
}
}
return 0;
}
BankAccount.cpp
#include <iostream>
#include "BankAccount.h"
usingnamespace std;
BankAccount account;
BankAccount::BankAccount()
{
}
BankAccount::~BankAccount()
{
}
void BankAccount::CreateAccount() // Used to create a new account for a customer
{
cout << "Please Enter The Name of The Account Holder: ";
getline(cin, account.AccountHolder);
cout << "Please Enter Your Account Number: ";
cin >> account.AccountID;
cout << "Please Enter Your Initial Balance: ";
cin >> account.AccountBalance;
cout << "\nYou Have Successfully Created An Account. ";
system("PAUSE");
}
void BankAccount::DisplayAccount() // Used to display a customers account
{
int AccountID;
cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
cin >> AccountID;
if (AccountID == account.AccountID)
{
cout << "The Account Holder is: " << account.AccountHolder << endl;
cout << "The Account Number is: " << account.AccountID << endl;
cout << "The Account's Balance is: " << account.AccountBalance << endl;
system("PAUSE");
}
elseif (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
{
cout << "Please Enter A Valid Account Number."<< endl;
system("PAUSE");
}
}
void BankAccount::Withdraw() // Allows The User To Withdraw Funds From Their Account
{
int AccountID;
double Withdraw;
double NewBalance;
cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
cin >> AccountID;
if (AccountID == account.AccountID)
{
cout << "The Account Holder is: " << account.AccountHolder << endl;
cout << "The Account Number is: " << account.AccountID << endl;
cout << "The Account's Balance is: " << account.AccountBalance << endl;
cout << "Please Enter The Amount You Wish To Withdraw: " ;
cin >> Withdraw;
NewBalance = account.AccountBalance - Withdraw;
account.AccountBalance = NewBalance;
cout << "Your New Balance Is: " << account.AccountBalance << endl;
system("PAUSE");
}
elseif (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
{
cout << "Please Enter A Valid Account Number." << endl;
system("PAUSE");
}
}
void BankAccount::Deposit() // Allows The User To Deposit Funds To Their Account
{
int AccountID;
double Deposit;
double NewBalance;
cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
cin >> AccountID;
if (AccountID == account.AccountID)
{
cout << "The Account Holder is: " << account.AccountHolder << endl;
cout << "The Account Number is: " << account.AccountID << endl;
cout << "The Account's Balance is: " << account.AccountBalance << endl;
cout << "Please Enter The Amount You Wish To Deposit: ";
cin >> Deposit;
NewBalance = account.AccountBalance + Deposit;
account.AccountBalance = NewBalance;
cout << "Your New Balance Is: " << account.AccountBalance << endl;
system("PAUSE");
}
elseif (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
{
cout << "Please Enter A Valid Account Number." << endl;
system("PAUSE");
}
}
void BankAccount::Loan() //Allow The User To Check Their Eligibility For A Loan And Take One Out If They Are.
{
double Loan;
double NewBalance;
cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
cin >> AccountID;
if (AccountID == account.AccountID)
{
cout << "The Account Holder is: " << account.AccountHolder << endl;
cout << "The Account Number is: " << account.AccountID << endl;
cout << "The Account's Balance is: " << account.AccountBalance << endl;
cout << "Please Enter The Amount You Wish To Take Out As A Loan: ";
cin >> Loan;
if (Loan > account.AccountBalance * 2)
{
cout << "You Are Not Eligible For A Loan Of This Size.\nPlease Enter A Value No More Than Double Your Current Balance"<< endl;
system("PAUSE");
}
elseif (Loan)
{
NewBalance = Loan + account.AccountBalance;
account.AccountBalance = NewBalance;
cout << "You Have Successfully Taken Out A Loan.\nYour New Balance Is: " << account.AccountBalance << endl;
system("PAUSE");
}
}
elseif (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
{
cout << "Please Enter A Valid Account Number." << endl;
system("PAUSE");
}
}
void BankAccount::Interest()
{
double Interest;
double NewBalance;
cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
cin >> AccountID;
if (AccountID == account.AccountID)
{
cout << "The Account Holder is: " << account.AccountHolder << endl;
cout << "The Account Number is: " << account.AccountID << endl;
cout << "The Account's Balance is: " << account.AccountBalance << endl;
Interest = account.AccountBalance * 0.03;
cout << "Your interest rate is 3%. You have earned a total of: " << Interest;
NewBalance = account.AccountBalance + Interest;
account.AccountBalance = NewBalance;
cout << "\nYour New Balance Is : " << account.AccountBalance << endl;
system("PAUSE");
}
elseif (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
{
cout << "Please Enter A Valid Account Number." << endl;
system("PAUSE");
}
}
void BankAccount::Overdraft()
{
//Incomplete
}
BankAccount.h
#pragma once
#include <string>
usingnamespace std;
class BankAccount
{
public:
BankAccount();
~BankAccount();
//Variables
private:
string AccountHolder;
double AccountBalance = 00.00;
int AccountID;
public:
void CreateAccount();
void Withdraw();
void Deposit();
void DisplayAccount();
void Loan();
void Interest();
void Overdraft();
};
// Function prototype. You need to provide
BankAccount * FindAccount (BankAccount accounts[], int acct_id, int num_accts); // return ptr to desired acct
int main()
{ constint MAX_ACCOUNTS = 10;
BankAccount accounts[MAX_ACCOUNTS];
int num_accounts = 0;
BankAccount * curr; // Pointer to current account
int end = 0;
int acct_id;
while (end != 1)
{ int op;
system("CLS");
op = Menu();
switch (op)
{
case 1:
curr = &accounts[num_accounts]; // point to next available accout
curr->CreateAccount();
num_accounts++; // Increment the number of accouts
break;
case 2:
cout << "Enter the account Id of the account you want to display";
cin >> acct_id;
curr = FindAccount (accounts, acct_id, num_accounts);
if (curr == nullptr)
{ cout << "Account not found" << endl;
}
else
{ curr->DisplayAccount();
}
break;
Edit: Added num_accounts as an argument to FindAccount().
Thank you for the help. Unfortunately I'm still at a loss. I've tried implementing this and it's not working at all now. Due to my own lack of knowing how to use it. Is there any more help you can give me?
Is FindAccount() a function I need to create in the BankAccount.cpp?
Yes, you need to create it. It should be in source.cpp since it is not a member function of BankAccount. It should loop through the accounts that have been created and look for a match on acct_id. When a matching account is found, it should return the address of that account. If not match is found, it should return nullptr.
On the line underneath case 1: "curr = accounts[num_accounts];"
Accounts is underlined red, any clue why this is?
Sorry. My mistake. It should have been:
curr = &accounts[num_accounts]; // Note the &. We need the address of the account
1 2 3 4 5 6 7
BankAccount * FindAccount (BankAccount accts[], int acct_id, int num_accts)
{ for (int i=0; i<num_accts; i++)
{ if (accts[i].AccountID == acct_id)
return &accts[i]; // Found it. Return pointer to account
}
returnnullptr; // Not found
}
Line 124 of your original post, account should not be a global.
bankaccount.cpp - numerous places - You should not be referring to the global account. A member function implicitly refers to a specific instance.