Account

This is an incomplete code.. i need to create 2 account whose balance can be entered by the user and both accounts separately be accessed and altered. kindly help me with this. having difficulty in the marked parts.

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

  #include <iostream>
#include <conio.h>
using namespace std;


	class Account
	{
	public:
		int balance;
		Account(int amount1)
		{
			cin>>amount1;
			if (amount1 < 0)
			{
				balance = 0;
				cout << "Error! Invalid Balance.\n\nBalance Automatically Set to 0." << endl;
			}
			else
			{
				balance = amount1;
				cout << "Balance Set" << endl;
			}
		}

		void credit(int amount)
		{
			balance = balance + amount;
		}

		void debit(int amount)
		{
			if (amount > balance)
			{
				cout << "Error! Cannot Withdraw Amount Exceeding Your Balance" << endl;
				return;
			}
			balance = balance - amount;
		}

		int getbalance()
		{
			return balance;
		}
	};



	int main()
	{
		Account bankaccount1(Account(balance))//here im not sure what to put in;

		int account;
		int balance;
		cout << "Enter The Account You Want to Access: ";
		cin >> account;
		cout << "\n" << endl;
		if (account = 1)
		{
			cout << "Bank Account 1 Accessed with Balance"<<bankaccount1.getbalance()//here too;

		}

		cout << endl;
i need to create 2 account whose balance can be entered by the user and both accounts separately be accessed and altered


hiya,

Remember that your class is a sort of "factory" for making objects, so I think you want something like this in your main:

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
#include <iostream>

class Account
{
public:
	Account(int balance) : m_balance(balance)
	{
		// other stuff (but i wouldn't do all that checking you've done
		// in a constructor).
	}

private:
	int m_balance;
};

int main()
{
	Account account1(100);
	Account account2(200);

	// now you have 2 accounts.	

	return 0;
}

yes. but will using it as
1
2
Account account1(100);
	Account account2(200);


enable the user to input the details and the amount in the account??
rather than just passing an amount on construction you could also pass in an account name. then ask the user for a name to search for. (which looks like what you might have to do).
if you do it like that you'll have to add your accounts to some kind of collection.
Last edited on
Topic archived. No new replies allowed.