c++:如何获取余额(在银行系统中)

发布于 2024-11-10 11:05:07 字数 2203 浏览 2 评论 0原文

我正在做这个项目银行系统 该系统跟踪客户在银行的账户。每个帐户都有一个号码、名称和余额。系统提供以下功能:创建新账户、提现、存款、关闭账户。
系统有如下界面:
选择:
1- 添加新帐户
2- 提款
3- 存款
4-取得平衡
5-退出
 当用户选择 1 时,系统会生成一个新 ID,然后要求用户输入该帐户的名称。初始余额设置为零。

 当用户选择 2 时,系统会要求用户输入帐户 ID 和要提取的金额。如果该金额大于余额,则会显示一条消息,提示余额不足,交易失败。如果余额足够,则会减少提取的金额。

 当用户选择 3 时。系统要求用户输入帐户 ID 和要存入的金额。系统将余额增加此金额。

 当用户选择 4 时,系统会要求用户输入帐户 ID,然后打印帐户名称和余额。

每次任务完成后,系统都会返回上面的主菜单,直到用户选择 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;
}
}

}

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 account 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;
}
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏雨凉 2024-11-17 11:05:07

问题 1:
您在方法中犯了一个拼写错误,该方法使您获得平衡&将您要调用的银行重命名为 Bank::disp_det()Bank::getbalance()

void Bank::getbalance()
{
    cout<<"Account Details";
    cout<<"Name of the depositor : "<<name<<endl;
    cout<<"Account Number        : "<<acno<<endl;
    cout<<"Balance               : $"<<balance<<endl;
}

问题 2:
您没有通过 Bank 的对象调用 Bank::getbalance,因为它是一个成员函数,您应该按如下方式调用它:

case 4: 
     obj.getbalance();
     break;

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;
作妖 2024-11-17 11:05:07

在情况 4 中,您应该调用 obj.getbalance()。而且它还没有写:看来你已经写了一个显示余额的 disp_det() 。尝试重命名。

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.

预谋 2024-11-17 11:05:07

这并不完全符合您的要求,因为案例标签具有不同的类型:

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;
}

要选择选项 '1''2',用户必须输入 31当选择为 int 时为 32。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文