Help with Account class program

I am taking my first course in C++ and been struggling with the recent arrays and structures stuff. Your help will be greatly appreciated.

This program must have 3 files:
1. Account.h which is the interface of the Account class.
2. Account.cpp which is the Account class implementation file.
3. AccountMain.cpp which is the Account class main function file.

The header file was given like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Account
{
private:
	//Private Data Members :
	int accountId; //Account ID
	double balance; //Current Balance of the account
	double annualInterestRate; //Current Annual Interest Rate of the Account (ex. 4.5)


public:
	//Public Member Functions : (Constructors)
	Account(); //Default Constructor (no parameters) 
	Account(int id, double bal, double interest); //Three-Parameter Constructor

	//Public Member Functions : (Setters)
	void setAccountId(int x); //Function sets id to x 
	void setBalance(double x); //Function sets balance to x 
	void setInterest(double x); //Function sets annualInterestRate to x 

	//Public Member Functions : (Getters)
	int getAccountId(); //Function returns accountId
	double getBalance(); //Function returns balance
	double getInterest(); //Function returns annualInterestRate

	//Public Member Functions :
	double getMonthlyInterestRate(); //Function calculates the monthly interest rate and returns the value 
	double getMonthlyInterest(); //Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
	bool withdraw(double amount); //Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
	void deposit(double amount); //Function adds the amount passed as a parameter to the current balance.

};


The problem says: Write a program that creates an array of 10 Account objects.
When creating each Account object use the following guidelines:

• Each object’s accountId should be the index of its position within the array.
• The balance of each Account object should be created from a random number generator function returning values between 10,000.00 and 20,000.00. This return value should be rounded to two decimal places.
• The interest rate of each Account object should be created from a random number generator function returning values between 1.5 and 5.0. This return value should be rounded to one decimal place.

The program should then ask the user to select an account number from 0 – 9 or -1 to exit the program.

If an account number is selected, then the user should be presented with some options. The five main bullets should form the menu presented. If a user makes any of the top five selections, then the menu should be represented. The menu should only not be represented with an entry of -1.

• Enter 1 to make a deposit
o Ask the user for the amount they wish to deposit
• Enter 2 to make a withdraw
o Ask the user for the amount they wish to withdraw
o Return if withdrawal was successful or unsuccessful depending on function return value
• Enter 3 to check balance
o Display the account’s current balance
• Enter 4 to check interest rate
o Display the account’s monthly and yearly interest rate
• Enter 5 to display account summary
o Display account id, balance, monthly interest rate, and monthly interest amount
• Enter -1 to return to the main menu
o This will return the user to the main menu prompting to select an account number

Here is a picture of an example output

http://s28.postimg.org/l7nwda9cd/Untitled.png
[url=http://postimg.org/image/7185i1yh5/][img]http://s28.postimg.org/7185i1yh5/Untitled.jpg[/img][/url]

Here is my work that is not working and I need to turn it in by tomorrow night:

Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/////////////
//Account.h//
/////////////

#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class Account
{
private:
	//Private Data Members :
	int accountId; //Account ID
	double balance; //Current Balance of the account
	double annualInterestRate; //Current Annual Interest Rate of the Account (ex. 4.5)


public:
	//Public Member Functions : (Constructors)
	Account(); //Default Constructor (no parameters) 
	Account(int id, double bal, double interest); //Three-Parameter Constructor

	//Public Member Functions : (Setters)
	void setAccountId(int x); //Function sets id to x 
	void setBalance(double x); //Function sets balance to x 
	void setInterest(double x); //Function sets annualInterestRate to x 

	//Public Member Functions : (Getters)
	int getAccountId(); //Function returns accountId
	double getBalance(); //Function returns balance
	double getInterest(); //Function returns annualInterestRate

	//Public Member Functions :
	double getMonthlyInterestRate(); //Function calculates the monthly interest rate and returns the value 
	double getMonthlyInterest(); //Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
	bool withdraw(double amount); //Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
	void deposit(double amount); //Function adds the amount passed as a parameter to the current balance.

};


Account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
///////////////
//Account.cpp//
///////////////

#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "Account.h"

using namespace std;

int id; double bal; double interest;
float mInterestRate; double yInterestRate; double mInterest;

Account accnt[10];

void setAccountId(int x)
{
	cout << "\nPlease enter an account number from 0 - 9" << endl;
	cin >> id;
	id = x;
}

int getAccountId()
{
	cout << "Account ID: " << id;
	return id;
}

void setBalance(double x)
{
	x = rand() % 20000 + 10000.00;
	bal = x;
}

void deposit(double amount)
{
	cout << "Enter amount to be deposited: " << endl;
	cin >> amount;
	bal += amount;
}

double getBalance()
{
	cout << "\nCurrent Balance: $" << bal << endl;
	return bal;
}

void setInterest(double x)
{
	x = rand() % 5 + 1.5;
	interest = x;
}

double getInterest()
{
	cout << "Yearly interest rate: " << interest << "%" << endl;
	return interest;
}

double getMonthlyInterestRate()
{
	mInterestRate = interest / 12;
	cout << "Monthly interest rate: " << mInterestRate << "%" << endl;
	return mInterestRate;
}

double getMonthlyInterest()
{
	mInterest = mInterestRate*bal;
	cout << "Monthly interest: $" << mInterest << endl;
	return mInterest;
}

bool withdraw(double amount)
{
	if (bal >= amount)
	{
		bal -= amount;
		return true;
	}
	else
	{
		return false;
	}
}

void start()
{
	cout << "\nPlease enter an account number from 0 - 9" << endl;
	cin >> id;


}

void options(Account accnt[], int id, double bal, double interest)
{
	int option;
	cout << "\nEnter 1 to make a deposit";
	cout << "Enter 2 to make a withdraw";
	cout << "Enter 3 to check balance";
	cout << "Enter 4 to check interest rate";
	cout << "Enter 5 to display account summary";
	cout << "Enter -1 to return to the main menu";
	cin >> option;

	if (option == 1)
	{
		deposit(bal);
		options(accnt, id, bal, interest);
	}
	else if (option == 2)
	{
		withdraw(bal);
		options(accnt, id, bal, interest);
	}
	else if (option == 3)
	{
		getBalance();
		options(accnt, id, bal, interest);
	}
	else if (option == 4)
	{
		cout << "\nMonthly interest rate: " << mInterestRate;
		cout << "Yearly interest rate: " << yInterestRate;
		options(accnt, id, bal, interest);
	}
	else if (option == 5)
	{
		cout << "\nAccount ID: " << id;
		cout << "Current Balance: " << bal;
		cout << "Monthly interest rate: " << mInterestRate;
		cout << "Monthly interest amount: " << mInterest;
		options(accnt, id, bal, interest);
	}
	else if (option == -1)
	{
		start();
		options(accnt, id, bal, interest);
	}
	else
	{
		cout << "\nInvalid Selection." << endl;
		options(accnt, id, bal, interest);
	}
}
AccountMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
///////////////////
//AccountMain.cpp//
///////////////////

#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "Account.cpp"

using namespace std;

int main()
{
	Account accnt[10]; 	//Create an array of 10 Account objects
	
	start(); 	//start program
	options(accnt, id, bal, interest); //display options
	

	system("pause");
	return 0;

}


When I build the solution, I am getting 22 Errors and 4 warnings. Most of the errors say that my member functions and some other functions are already defined in Account.obj

The warnings are about the conversion from double to float and that the options function will cause runtime stack overflow.

I am so tired of this and hope some can help me.
acountmain.cpp
-------------------
line 13: Include account.h, not account.cpp. This is what is causing linker errors.

account.cpp
--------------
All your implementations of member functions need to be Account::functionname()

line 17-20: These are going to cause a doubly defined symbols at link time since they also exiss in accountmain.cpp

line 25-26: You accept id from the user, then you overlay id with the passed argument. What's the point of asking the user?

line 54-58: You pass x as the argument, then overlay it with a random number. What's the point of passing an argument?

lines 101-151: start() and options() are not member functions and do not belong in account.cpp. They should be in accountmain.cpp.

lines 115,120,125,131,139,144,149: Recursion is not really the best way to handle displaying the menu again. Every time you display the menu, you're going to push another stack frame onto the stack. Even when you want to exit the menu (141-145), you push another stack frame. You have no path out of this function that doesn't push another stack frame. You will eventually run out of stack space. This is better suited to a simple loop.

Lines 114,119,124: These function calls must reference an Account instance.

Your header file (27-28) shows two Account constructors, but I don't see an implementation for either of them.

This should get you closer. It's not complete, but it does compile. You still have work to do.
 
//  account.h is unchanged 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
///////////////
//Account.cpp//
///////////////

#include <iostream>
#include "Account.h"

using namespace std;

Account::Account ()     //  Missing constructor
{   accountId = 0;
    balance = 0;
    annualInterestRate = 0;
}

Account::Account(int id, double bal, double interest)   //  missing constructor
{   accountId = id;
    balance = bal;
    annualInterestRate = interest;
}       

void Account::setAccountId (int x)
{   accountId = x;
}

int Account::getAccountId()
{   return accountId;
}

void Account::setBalance(double x)
{   balance = x;
}

void Account::deposit(double amount)
{   cout << "Enter amount to be deposited: " << endl;
	cin >> amount;
	balance += amount;
}

double Account::getBalance()
{   return balance;
}

void Account::setInterest(double x)
{	annualInterestRate = x;
}

double Account::getInterest()
{   return annualInterestRate;
}

double Account::getMonthlyInterestRate()
{   double  mInterestRate;

	mInterestRate = annualInterestRate / 12;
	return mInterestRate;
}

double Account::getMonthlyInterest()
{   double mInterestRate;
    double mInterest;
    
	mInterestRate = annualInterestRate / 12;	
	mInterest = mInterestRate * balance;
	return mInterest;
}

bool Account::withdraw(double amount)
{   if (balance >= amount)
	{   balance -= amount;
		return true;
	}
	else
	{   return false;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
///////////////////
//AccountMain.cpp//
///////////////////

#include <iostream>
#include <cstdlib>
#include "Account.h"

using namespace std;

int get_account_id()     //  renamed from start()
{   int     id;

    cout << "\nPlease enter an account number from 0 - 9" << endl;
	cin >> id;
	return id;
}

int get_choice ()
{   int option;

	cout << "Enter 1 to make a deposit" << endl;
	cout << "Enter 2 to make a withdraw" << endl;
	cout << "Enter 3 to check balance" << endl;
	cout << "Enter 4 to check interest rate" << endl;
	cout << "Enter 5 to display account summary" << endl;
	cout << "Enter 0 to exit" << endl;
	cin >> option;
    return option;
}
    	 
void process_account (Account & accnt, int option)
{   double  amt = 0;

    switch (option)
    {
    case 1:
	    accnt.deposit (amt);    //  Need to determine amt somehow
        break;
	case 2: 
	    accnt.withdraw (amt);   //  Need to determine amt somehow
        break;
    case 3: 
	    accnt.getBalance();
        break;
	case 4: 
	    cout << "Monthly interest rate: " << accnt.getMonthlyInterest() << endl;
		cout << "Yearly interest rate: " << accnt.getInterest() << endl;
		break;
	case 5:
	    cout << "\nAccount ID: " << accnt.getAccountId() << endl;
		cout << "Current Balance: " << accnt.getBalance() << endl;
		cout << "Monthly interest rate: " << accnt.getMonthlyInterestRate() << endl;
		cout << "Monthly interest amount: " << accnt.getMonthlyInterest() << endl;		
		break;
	default:    
	    cout << "nInvalid Selection." << endl;		
	}
}

int main()
{   int id;  
    int choice;  
	Account accnt[10]; 	//Create an array of 10 Account objects
	
	while (choice = get_choice())
	{   id = get_account_id(); 	    
	    process_account (accnt[id], choice); 
    }
	system("pause");
	return 0;
}
Last edited on
@AbstractionAnon
You're very smart!! Thank you so much! I was able to finish the program with your help. I also needed a correct random number generator function for balance and annual interest rate, and setting number of decimals required, but figured it out with some research. Thank you again.
Topic archived. No new replies allowed.