Hello! I have written written my own class, which includes a header file and a .cpp file. I have also written the driver for this program. Everything looks fine to me, but I am receiving about 20 random errors. I will post my code for all 3 files but I believe it is a problem that can be diagnosed using just the driver.
The program is a bank account program that adds new accounts, withdraws, deposits, updates, displays, etc. Any help would be greatly appreciated! Thanks.
HEADER
#include <iostream>
#include <string>
using namespace std;
class BankAccount
{
public:
BankAccount();
void deposit( double amt );
void withdraw( double amt );
void update();
int getID();
void setID( int anID );
double getBalance();
void setBalance( double amt );
double getInterestRate();
void setInterestRate( double amt );
string getFirstName();
void setFirstName( string theName );
string getLastName();
void setLastName( string theName );
string getFullName();
void display();
private:
string firstName, lastName;
double interestRate;
int ID;
double balance;
};
CLASS DEFINITION
#include "BankAccount.h"
/*
Default Constructor
*/
BankAccount::BankAccount()
{
ID = 0;
balance = 0.0;
interestRate = 0.0;
}
/*
Adds deposit amount to current balance
*/
void BankAccount::deposit( double amt )
{
balance = balance + amt;
}
/*
Withdraws amount from current balance
if there are sufficient funds.
Fails quietly for insufficient funds.
*/
void BankAccount::withdraw( double amt )
{
if( balance < amt ) return;
balance = balance - amt;
}
/*
Getter (accessor) for balance
*/
double BankAccount::getBalance()
{
return balance;
}
/*
Setter (mutator) for balance
*/
void BankAccount::setBalance( double amt )
{
balance = amt;
}
/*
Adds interest to account using current interest rate
*/
void BankAccount::update()
{
balance = balance + balance * interestRate;
}
/*
Getter for ID
*/
int BankAccount::getID()
{
return ID;
}
/*
Setter for ID
*/
void BankAccount::setID( int anID )
{
ID = anID;
}
/*
Getter for balance
*/
double BankAccount::getInterestRate()
{
return interestRate;
}
/*
Setter for interest rate
*/
void BankAccount::setInterestRate( double amt )
{
interestRate = amt;
}
/*
Getter for first name
*/
string BankAccount::getFirstName()
{
return firstName;
}
/*
Setter for first name
*/
void BankAccount::setFirstName( string theName )
{
firstName = theName;
}
/*
Getter for last name
*/
string BankAccount::getLastName()
{
return lastName;
}
/*
Setter for last name
*/
void BankAccount::setLastName( string theName )
{
lastName = theName;
}
/*
Getter for full name
*/
string BankAccount::getFullName()
{
return lastName + ", " + firstName;
}
/*
Displays info about about to console.
*/
void BankAccount::display()
{
cout << "ID : " << ID << endl;
cout << "Name : " << getFullName() << endl;
cout << "Balance : $" << balance << endl;
cout << "Interest Rate : " << interestRate << " %" << endl;
}
DRIVER
#include "BankAccount.h"
int getChoice();
void addBankAccount();
void withdrawAccount();
void depositAccount();
void updateAccounts();
void displayAccount();
void displayBalance();
void displayInfo();
int find(count, accts, id);
double tempRate;
double balance;
int count[10];
BankAccount accts[10];
int nextID = 100;
void main()
{
int choice;
do{
choice = getChoice();
if( choice == 1 ) addBankAccount();
else if( choice == 2 ) withdrawAccount();
else if( choice == 3 ) depositAccount();
else if( choice == 4 ) updateAccounts();
else if( choice == 5 ) displayAccount();
else if( choice == 6 ) displayBalance();
else if( choice == 7 ) displayInfo();
if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
} while( choice != 8 );
}
//This function displays the calculation menu.
int getChoice()
{
int c;
cout << "----------------------\n";
cout << "1 - Add a new bank account.\n";
cout << "2 - Withdraw from a specific bank account.\n";
cout << "3 - Deposit to a specific account.\n";
cout << "4 - Update all accounts which will add a fixed interest rate to each account.\n";
cout << "5 - Display one specific bank accout.\n";
cout << "6 - Display the average balance for all of the accounts.\n";
cout << "7 - Display the current information for all accounts.\n";
cout << "8 - Terminate the program.\n";
cout << "----------------------\n";
cout << "Enter number of choice --> ";
cin >> c;
return c;
}
//This function adds a bank account for a new member.
void addBankAccount()
{
int count = 0;
string temp;
cout << "Please enter first name.";
cin >> temp;
accts[count].setFirstName(temp);
cout << "Please enter last name.";
cin >> temp;
accts[count].setLastName(temp);
cout << "Please enter balance.";
cin >> balance;
accts[count].setBalance(balance);
cout << "Please enter interest rate (decimal form).";
cin >> tempRate;
accts[count].setInterestRate(tempRate);
accts[count].setID(nextID);
nextID = nextID + 1;
count++;
}
// This function withdraws an amount from a specific account.
void withdrawAccount()
{
int slot;
double amt;
int id;
cout << "Enter ID number.";
cin >> id;
int slot = find(count, accts, id)
cout << "Enter amount to withdraw.";
cin >> amt;
accts[slot].withdraw(amt);
}
//This function deposits an amount into a specific account.
void depositAccount()
{
int slot;
double amt;
int id;
cout << "Enter ID number.";
cin >> id;
int slot = find(count, accts, id)
cout << "Enter amount to deposit.";
cin >> amt;
accts[slot].deposit(amt);
}
//This function updates all accounts using a fixed interest rate.
void updateAccounts()
{
for (int i=0; i<count; i++)
accts[i].update();
}
//This function displays one bank account.
void displayAccount()
{
int target;
cout << "Enter account number.";
cin >> target;
accts[target].display();
}
//This function displays the average balance of all accounts.
void displayBalance()
{
}
//This function displays the current information for all accounts.
void displayInfo()
{
for (int i=0; i<count; i++)
accts[i].display();
}
int find(count, accts, id)
{
int index = 0;
bool found = false;
while ((!found) && (index < count))
if (id == accts[index])
found = true;
else
index++;
if (found)
return index;
else
return -1;
}