Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I tried to solve the following program:-

Implement a class Bank. This bank has two objects, checking and savings,
of the type Account. Implement the following member functions:

void deposit(double amount, char account)
void withdraw(double amount, char account)
void print_balances()
Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected.

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class bank
{
private:
double a1,a2,sa,cu;
char ac,c,s;
public:
bank()
{
sa=0;
cu=0;
}
void deposit(double amount, char account);
void withdraw(double amount, char account);
void print_balances();
};
void bank::deposit(double amount1, char account)
{
a1=amount1,
ac=account;
if (ac=='s')
{
sa=sa+a1;

}
else if(ac=='c')
{
cu=cu+a1;
}
}
void bank::withdraw(double amount2, char account)
{
a2=amount2,
ac=account;
if (ac=='s')
{
sa=sa-a2;
}
else if(ac=='c')
{
cu=cu-a2;
}
}
void bank::print_balances()
{
cout<<"\nthe money in the savings account is: "<<sa;
cout<<"\nthe money in the current  account is: "<<cu;
}
main()
{
char account;
double amount1,amount2;
int l;
clrscr();
bank b;
do
{

cout<<"\nenter your choice";
cout<<"\n1.deposit";
cout<<"\n2. withdraw";
cout<<"\n3.print balances";
cout<<"\n4.exit";
cout<<"\n";
cin>>l;
switch(l)
{
case 1:
cout<<"\nenter account type: ";
cin>>account;
cout<<"\nenter amount to be deposited :" ;
cin>>amount1;
b.deposit(amount1, account);
break;
case 2:
cout<<"\nenter account type: ";
cin>>account;
cout<<"\nenter amount to be withdrawn :" ;
cin>>amount2;
b.deposit(amount2, account);
break;
case 3:
b.print_balances();
break;
case 4:
cout<<"\nthanx ";

}
}while(l!=4);
getch(); 
}

/* now, when I use the void withdraw(double amount, char account); function and withdraw the money, instead of decreasing, the money increases. suppose I have deposited 5 in my savings account. After that I withdraw 5 from my account, the money in the savings account should be 0 but it is 10, i.e instead of decreasing 5 in my account, it increases. */
Posted
Updated 20-Jan-17 10:58am
Comments
[no name] 20-Jan-17 8:36am    
Look at your code
cout<<"\nenter amount to be withdrawn :" ;
cin>>amount2;
b.deposit(amount2, account);

You seriously can't see the problem here? Learn how to use the debugger and you will be able to find and fix these types of problems yourself.
OriginalGriff 20-Jan-17 8:42am    
I'd post that as the solution if I was you... :laugh:
José Amílcar Casimiro 20-Jan-17 9:46am    
I would like to be a client of your bank, any transaction would make a deposit in my account.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 12959299 21-Jan-17 4:24am    
thanks for your suggestion.
Just to get this off the un-answered list
Look at your code
cout<<"\nenter amount to be withdrawn :" ;
cin>>amount2;
b.deposit(amount2, account);

Learn how to use the debugger and you will be able to find and fix these types of problems yourself.
 
Share this answer
 
Comments
Member 12959299 21-Jan-17 4:21am    
silly mistake. yes, its working. Thanks for your precious time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900