a question about class

hi,
I wrote this code,but It doesn't work when I complied it.please tell me that what is it's proplem and solve it.
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
#include <iostream>
using namespace std;
class account
{
 
public:
int balance;
account(int );
void credit(int );
void debit(int );
int getbalance();
void printbalance();
};
 
account::account(int i)
{
if (i<0)
{
balance=0;
cout<<"Error=>>Value of balance is negative";
}
else
{
i=balance;
}
}
void account::credit(int j)
{
balance += j;
}
void account::debit(int k)
{
if (k>balance)
{
cout<<"Error :Taking Money is more from Total Money";
}
else
{
balance -= k;
}
}
int account::getbalance()
{
return balance;
}
void account::printbalance()
{
cout<<balance;
}
int main()
{
	int i,j,k;
cin >> i>>j>>k;
account ac(i) ;
ac.credit(j);
ac.printbalance();
ac.debit(k);
ac.printbalance();
 cin.get();
	  cin.get();

}
What do you mean by 'it doesn't work'?
when I enter 3 numbers (value of balance,taking money, adding money) ,program shows negative digits with this message:
Error :Taking Money is more from Total Money
In the constructor

account::account(int i)
{
if (i<0)
{
balance=0;
cout<<"Error=>>Value of balance is negative";
}
else
{
i=balance;
}
}


you shall change statement

i=balance;

to

balance = i;

Topic archived. No new replies allowed.