c++:how to get balance (in banking system)
i'm doing this project Banking System This system tracks customers’ accounts in a bank. Each account has a number, name, and balance. The system provides the following functionalities: create new account, withdraw, deposit, and close account.
The system has the following interface: Choose: 1- Add new account 2- Withdraw 3- Deposit 4- Get Balance 5- Exit When the user chooses 1, the system generates a new ID, and then asks the user to enter a name for that account. The initial balance is set to zero. When the user chooses 2, the system asks the user to enter account ID and amount to be withdrawn. If this amount is greater than the balance, a message is displayed that this transaction failed because insufficient balance. If balance is enough, it decreases by amount to be withdrawn.
When the user chooses 3. The system asks the user to enter account ID and amount to be deposited. System increases balance by this amount.
When the user chooses 4, the system asks the user to enter acco开发者_StackOverflow中文版unt ID then prints account’s name and balance.
Each time a task is completed the system gets back to the main menu above until the user chooses 5.
# include <iostream>
#include <string>
using namespace std;
# include<iomanip>
class Bank
{
private:
char name;
int acno;
float balance;
public:
void newAccount();
void withdraw();
void deposit();
void getbalance();
void disp_det();
};
//member functions of bank class
void Bank::newAccount()
{
cout<<"New Account";
cout<<"Enter the Name of the depositor : ";
cin>>name;
cout<<"Enter the Account Number : ";
cin>>acno;
cout<<"Enter the Amount to Deposit : ";
cin >>balance;
}
void Bank::deposit()
{
float more;
cout <<"Depositing";
cout<<"Enter the amount to deposit : ";
cin>>more;
balance+=more;
}
void Bank::withdraw()
{
float amt;
cout<<"Withdrwal";
cout<<"Enter the amount to withdraw : ";
cin>>amt;
balance-=amt;
}
void Bank::disp_det()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
// main function , exectution starts here
void main(void)
{
Bank obj;
int choice =1;
while (choice != 5 )
{
cout<<"Enter \n 1- to create new account \n 2- Withdraw\n 3- Deposit \n 4- get balance\n 5 Exit"<<endl;
cin>>choice;
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
}
}
Problem 1:
You have made a typo in method which gets you the balance & the one which you are calling, rename Bank::disp_det()
to Bank::getbalance()
void Bank::getbalance()
{
cout<<"Account Details";
cout<<"Name of the depositor : "<<name<<endl;
cout<<"Account Number : "<<acno<<endl;
cout<<"Balance : $"<<balance<<endl;
}
Problem 2:
You are not calling Bank::getbalance
through an object of Bank
, Since it is a member function you should call it as follows:
case 4:
obj.getbalance();
break;
In case 4, you should call obj.getbalance()
. And it's not written yet: it seems you have written a disp_det()
instead that shows the balance. Try renaming.
This isn't doing exactly what you want, as the case labels have different types:
switch(choice)
{
case '1' :obj.newAccount();
break;
case '2' :obj.withdraw();
break;
case 3: obj.deposit();
break;
case 4: getbalance();
break;
case 5:
break;
default: cout<<"Illegal Option"<<endl;
}
To select options '1'
or '2'
, the user will have to type 31 and 32 when choice is an int
.
精彩评论