Array with 10 objects and trouble with 2 errors

Hello everyone, first i apologize for creating multiple threads i will make sure to keep everything on one thread from now on. I an have the program compiling but Im not getting the expected values for my variables. It is supposed to have no pointers just an object array, functions, constructors, and data abstraction. For Example the balance varaible is an outrageous number when its supposed to be between 10,000 and 20,000. If anyone can help me spot the mistake it would be very appreciated. Thanks.
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//headerfile "Account.h"
class Account // variable declarations and function prototypes
{
private:
	int accountId;
	double balance;
	double annualInterestRate;

public: //contsructors
	Account() {}

	Account(int id, double balance, double interest)
		: accountId(id), balance(balance), annualInterestRate(interest) {}

public: //setters
	void setAccountId(int x);
	void setBalance(double x);
	void setInterest(double x);
public: // getters
	int getAccountId();
	double getBalance();
	double getInterest();
public:
	double getMonthlyInterestRate();
	double getMonthlyInterest();
	bool withdraw(double amount);
	void deposit(double amount);


};

//source code file for header "Account.cpp"

#include "Account.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;


void Account::setAccountId(int x)
{
	cout << "Please enter an account number from 0-9 : ";
	cin >> accountId;
	cout << endl;
	accountId = x;
};
void Account::setBalance(double x)
{
	x = rand() % 10000 + 10000;
	balance= x;
};
void Account::setInterest(double x)
{
	annualInterestRate = rand() % 3 + 1.5;
	annualInterestRate = x;
};

int Account::getAccountId()
{
	return accountId;
}
double Account::getBalance()
{
	return balance;
}
double Account::getInterest()
{
	
	return annualInterestRate;
}

double Account::getMonthlyInterestRate()
{
	return annualInterestRate / 12;
}
double Account::getMonthlyInterest()
{
	return getMonthlyInterestRate() * balance;
}
bool Account::withdraw(double amount)
{
	if (balance >= amount)
		return true;
	else
		return false;
}
void Account::deposit(double amount)
{
	setBalance(balance + amount);
}

//Main file "AccountMain.cpp"
  #include <iostream>
#include <string>
#include "Account.h"
#include <iomanip>
#include <cstdlib> 

using namespace std;

void menu();

int main()
{
	int id = 0;
	int amount = 0;
	int selection = 0;
	Account object;

	const int N = 10;
	Account Account[N];

	
	
	for (int i = 0; i < N; i++)
	{
		
		int id= i;
		double balance = Account[i].getBalance();
		double annualInterestRate = Account[i].getInterest();

		Account[i] = Account::Account(id, balance, annualInterestRate);
		object.setAccountId(id);
		menu();
		while (cin >> selection && selection != -1)
		{
			switch (selection)
			{
			case 1:
				cout << "Please enter a deposit amount: $ ";
				cin >> amount;
				cout << endl;
				break;
			case 2:
				cout << "Please enter the amount you wish to withdraw: $ ";
				cin >> amount;
				cout << endl;
				Account[i].withdraw(amount);
				break;
			case 3:
				cout << "The accounts current balance is : $"
					<< fixed
					<< setprecision(3)
					<< Account[i].getBalance()
					<< endl;
				break;
			case 4:
				cout << "Monthly interest rate : "
					<< fixed
					<< setprecision(2)
					<< Account[i].getMonthlyInterestRate()
					<< "%"
					<< endl;
				cout << "Yearly Interest rate : "
					<< fixed
					<< setprecision(2)
					<< Account[i].getInterest()
					<< "%"
					<< endl;
				break;
			case 5:
				cout << "Account ID : "
					<< id
					<< endl;
				cout << "Account Balance : $"
					<<fixed
					<<setprecision(2)
					<< Account[i].getBalance()
					<< endl;
				cout << "Monthly Interest rate : "
					<< Account[i].getMonthlyInterest()
					<< "%"
					<< endl;
				cout << "Monthly Interest amount earned: $"
					<< Account[i].getMonthlyInterest()
					<< endl;
				break;
			default:
				cout << "Invalid" << endl;
			}
			menu();
		}

	}




	
		
	
	return 0;
}
void menu()
{
	cout << "Enter 1 to make a deposit" << endl;
	cout << "Enter 2 to make a withraw" << 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 -1 to return to main menu" << endl;

}
Last edited on
closed account (48T7M4Gy)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>

using namespace std;

class Account
{
private:
    int accountId;
    double balance;
    double annualInterestRate;
    
public: //contsructors
    Account();
    Account(int id, double bal, double interest);
public: //setters
    void setAccountId(int x);
    void setBalance(double x);
    void setInterest(double x);
public: // getters
    int getAccountId();
    double getBalance();
    double getInterest();
public:
    double getMonthlyInterestRate();
    double getMonthlyInterest();
    bool withdraw(double amount);
    void deposit(double amount);
    
    
};

Account::Account()// bodies of functions
{
    
}
Account::Account(int id, double bal, double interest)
{
    
}

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

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

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

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

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

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

double Account::getMonthlyInterestRate()
{
    return annualInterestRate / 12;
}

double Account::getMonthlyInterest()
{
    return getMonthlyInterestRate() * balance;
}

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

void Account::deposit(double amount)
{
    setBalance(balance + amount);
}

void menu();
int AccountNumber();

int main()
{
    int id = 0;
    int amount = 0;
    int selection = 0;
    Account object;
    
    menu();
    while (cin >> selection && selection != -1)
    {
    
        switch (selection)
        {
            case 1:
                cout << "Please enter a deposit amount: $ \n";
                cin >> amount;
                cout << endl;
                object.deposit(amount);
                break;
            case 2:
                cout << "Please enter the amount you wish to withdraw: $ \n";
                cin >> amount;
                cout << endl;
                object.withdraw(amount);
                break;
            case 3:
                cout << "The accounts current balance is : $\n"
                << object.getBalance()
                << endl;
                break;
            case 4:
                cout << "Monthly interest rate : \n"
                << object.getMonthlyInterestRate()
                <<"%"
                << endl;
                cout << "Yearly Interest rate : \n"
                << object.getInterest()
                <<"%"
                << endl;
                break;
            case 5:
                cout << "Account ID : \n"
                << id
                << endl;
                cout << "Account Balance : $\n"
                << object.getBalance()
                << endl;
                cout << "Monthly Interest rate : \n"
                << object.getMonthlyInterestRate()
                << "%"
                << endl;
                cout << "Monthly Interest amount earned: $\n"
                << object.getMonthlyInterest()
                << endl;
                break;
            default:
                cout << "Invalid\n";
        }
        menu();
    }
    
    return 0;
}

void menu()
{
    cout << "Enter 1 to make a deposit" << endl;
    cout << "Enter 2 to make a withraw" << 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 -1 to return to main menu" << endl;
    
}
int AccountNumber()
{
    int id;
    cout << "Please enter an account number from 0 - 9";
    cin >> id;
    return id;
}
Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
1
Please enter a deposit amount: $ 
200

Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
3
The accounts current balance is : $
200
Enter 1 to make a deposit
Enter 2 to make a withraw
Enter 3 to check balance
Enter 4 to check interest rate
Enter 5 to display account summary
Enter -1 to return to main menu
Last edited on
closed account (48T7M4Gy)
You will see that this now runs. You need to fix up your constructors and add a destructor.

To create an array of accounts all you do is Account array_of_accounts[10]; or similar. Then each account i, is addressed by array_of_account[i]

Your menu will need to ask for the account number and then perform the other operations on that object. ie the array is declared in main().
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/213251/#msg995467
Does anyone see why the balance is not correct? The program is compiling but the values are off.
Hi, caltice.
Please, have a look at the following code and ask if specific things are not clear:

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
#ifndef ACCOUNT_H
#define ACCOUNT_H

// The proprierties not initialized by the user will be filled with
// random values.
class Account
{
private:
   int accountId {0};
   double balance {0.0};
   double annualInterestRate {0.0};
   static bool is_seeded;  // set to true after srand() initialization
                           // to avoid further initializations

   void setInitialAccountId();
   void setInitialBalance();
   void setInitialIntRate();

public: //contsructors
   Account();
   Account(int id);
   Account(int id, double balanceArg);
   Account(int id, double balanceArg, double interest);

   //setters
   void setAccountId(int value = 0);
   void setBalance(double value = 0);
   void setInterest(double value = 0);
   // getters
   int getAccountId();
   double getBalance();
   double getInterest();

   double getMonthlyInterestRate();
   double getMonthlyInterest();
   bool withdraw(double amount);
   void deposit(double amount);
};

#endif // ACCOUNT_H 


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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "Account.h"

bool Account::is_seeded = false;

void Account::setInitialAccountId()
{
   accountId = std::rand() % 9;
}

void Account::setInitialBalance()
{
   balance = std::rand() % 10000 + 10000;
}

void Account::setInitialIntRate()
{
   annualInterestRate = std::rand() % 3 + 1.5;
}

Account::Account()
{
   if(!is_seeded) {
      std::srand(std::time(0));
      is_seeded = true;
   }
   setInitialAccountId();
   setInitialBalance();
   setInitialIntRate();
}

Account::Account(int id)
{
   accountId = id;
   if(!is_seeded) {
      std::srand(std::time(0));
      is_seeded = true;
   }
   setInitialBalance();
   setInitialIntRate();
}

Account::Account(int id, double balanceArg)
{
   accountId = id;
   balance = balanceArg;
   if(!is_seeded) {
      std::srand(std::time(0));
      is_seeded = true;
   }
   setInitialIntRate();
}

Account::Account(int id, double balanceArg, double interest)
   : accountId {id}, balance {balanceArg}, annualInterestRate {interest}
{}

void Account::setAccountId(int value)
{
   if(value) {
      accountId = value;
   } else {
      std::cout << "Please enter an account number from 0-9 : ";
      std::cin >> accountId;
   }
}

void Account::setBalance(double value)
{
   if(value) {
      balance = value;
   } else {
      std::cout << "Please enter a balance between 10,000 and 20,000: ";
      std::cin >> balance;
   }
}

void Account::setInterest(double value)
{
   if(value) {
      annualInterestRate = value;
   } else {
      std::cout << "Please enter an annual interest rate: ";
      std::cin >> annualInterestRate;
   }
}

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

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

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

double Account::getMonthlyInterestRate()
{
   return annualInterestRate / 12;
}

double Account::getMonthlyInterest()
{
   return getMonthlyInterestRate() * balance;
}

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

   return false;
}

void Account::deposit(double amount)
{
   setBalance(balance + amount);
}


main.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
#include <iostream>
#include <string>
#include <iomanip>
#include "Account.h"

void menu();

int main()
{
   const int TEST_ACCOUNTS = 10;

   // Declaring an array of Account(s).
   Account accounts[TEST_ACCOUNTS];

   // Let's look inside them:
   for(int i = 0; i< TEST_ACCOUNTS; i++) {
      std::cout << "\nAccount n. " << i
                << "\naccountId = " << accounts[i].getAccountId()
                << "; balance = " << accounts[i].getBalance()
                << "; interest rate = " << accounts[i].getInterest()
                << '\n';
   }
   // Ok, it looks we have all date in the ranges we decided.
   // BTW, it happened because the Account::Account() constructor
   // has been invoked for every account.

   // The first problem is: more than one account could have the
   // same accountId, which is not logic.

   // Now, let's ask our user what s/he wants:
   int selection = 0;
   do {
      menu();
      std::cin >> selection;

      int index = 0;
      if(selection > 0) {
         std::cout << "Please, indicate on which account do you want "
                      "to operate [0-9]: ";
         std::cin >> index;
      }

      double amount = 0.0;
      switch (selection)
      {
         case -1:
            std::cout << "Goodbye.\n";
            break;
         case 1:
            std::cout << "Please enter a deposit amount: $ ";
            std::cin >> amount;
            accounts[index].deposit(amount);
            break;
         case 2:
            std::cout << "Please enter the amount you wish to withdraw: $ ";
            std::cin >> amount;
            if(accounts[index].withdraw(amount)) {
               std::cout << "Ok, you have taken your money back\n";
            } else {
               std::cout << "Sorry, you don't have such a sum!\n";
            }
            break;
         case 3:
            std::cout << "This account current balance is : $"
                      << std::fixed << std::setprecision(3)
                      << accounts[index].getBalance()
                      << std::endl;
            break;
         case 4:
           std:: cout << "Monthly interest rate on this account: "
                      << std::fixed  << std::setprecision(2)
                      << accounts[index].getMonthlyInterestRate()
                      << "%; yearly interest rate : "
                      << std::fixed << std::setprecision(2)
                      << accounts[index].getInterest()
                      << "%"
                      << std::endl;
            break;
         case 5:
            std::cout << "Account ID : " << accounts[index].getAccountId()
                      << "\nAccount Balance : $"
                      << std::fixed << std::setprecision(2)
                      << accounts[index].getBalance()
                      << "\nMonthly Interest rate : "
                      << accounts[index].getMonthlyInterest()
                      << "%; monthly Interest amount earned: $"
                      << accounts[index].getMonthlyInterest()
                      << std::endl;
            break;
         default:
            std::cout << "Invalid\n";
            break;
      }

   } while (selection != -1);

   return 0;
}

void menu()
{
   std::cout << "\nEnter 1 to make a deposit\n";
   std::cout << "Enter 2 to make a withraw\n";
   std::cout << "Enter 3 to check balance\n";
   std::cout << "Enter 4 to check interest rate\n";
   std::cout << "Enter 5 to display account summary\n";
   std::cout << "Enter -1 to return to main menu\n";
}


Please, do not make vague and general questions.
The first step could be to make a simplified version of you code which reproduces the errors you come up against.
Topic archived. No new replies allowed.