C++ 银行应用程序不持有多个帐户
C++ Banking application not holding more than one account
您好,我一直在开发一个C++银行应用程序,该应用程序应该能够在所有相关字段中持有多个帐户。我遇到了几个问题:
- 在显示或显示信息
- 功能中显示帐户信息时,不会显示名字的第一个字母和中间的首字母。
- 创建帐户时,只能搜索最新的帐户并将其显示在显示数据中。我知道这需要一个数组才能实现,我只是不确定是否正确实现了这一点。
感谢您的帮助。任何意见不胜感激!
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
class BankAccount{
double Balance = 0.0;
char ans;
public:struct Name{
char Last_Name[50];
char First_Name[50];
char Middle_Initial[5];
}Name;
public:struct Account{
char Type[1];
int Account_Number;
}Account;
public:
void CreateAccount();
void Withdraw();
void Deposit();
void Display();
void ShowInfo();
int Menu();
};
void BankAccount::CreateAccount(){
do
{
cout << "nEnter account number: ";
cin >> Account.Account_Number;
cout << "nEnter the last name for the account: ";
cin.ignore();
cin.getline(Name.Last_Name, 50);
cout << "nEnter the first name for the account: ";
cin.ignore();
cin.getline(Name.First_Name, 50);
cout << "nEnter the Middle initial for the account: ";
cin.ignore();
cin.getline(Name.Middle_Initial, 5);
cout << "nEnter the type of account (C/S) : ";
cin >> Account.Type;
cout << "nEnter the initial balance of the account: ";
cin >> Balance;
cout << "nnAccount Created.";
cout << "nnCreate new account? (Y/N) : ";
cin >> ans;
while (ans != 'Y' && ans != 'N'){
cout << "Invalid input. Create new account? (Y/N) : ";
cin >> ans;
}
cout << endl;
} while (ans != 'N');
};
void BankAccount::Withdraw(){
int actNum;
double amount;
cout << "Enter the account number for the account that you wish to withdraw funds: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Enter the amount you would like to withdraw: ";
cin >> amount;
Balance = Balance - amount;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::Deposit(){
int actNum;
double amount;
cout << "Enter the account number for the account that you wish to deposit funds: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Enter the amount you would like to deposit: ";
cin >> amount;
Balance = Balance + amount;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::Display(){
int actNum;
cout << "Enter the account number for the account that you wish to display account information for: ";
cin >> actNum;
if (actNum == Account.Account_Number){
cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
cout << "Account Number: " << Account.Account_Number << endl;
cout << "Account Type (Checking / Savings): " << Account.Type << endl;
cout << "Account Balance: $" << Balance << endl;
}
else if (actNum != Account.Account_Number){
cout << "No account found under that number! Try again!";
}
}
void BankAccount::ShowInfo(){
cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
cout << "Account Number: " << Account.Account_Number << endl;
cout << "Account Type (Checking / Savings): " << Account.Type << endl;
cout << "Account Balance: $" << Balance << endl;
}
int main(int argc, char *argv){
BankAccount ob;
char ch;
cout << "Welcome to Console Banking Application V 1.0!";
cout << "nSelect an item from the list below by entering the corresponding letter.";
do{
cout << "nn A. Create Account n B. Withdraw n C. Deposit n D. Show Account Details nn Q. Exit Applicationnn";
ch = ob.Menu();
switch (ch){
case 'A':
case 'a': ob.CreateAccount();
ob.ShowInfo();
break;
case 'B':
case 'b': ob.Withdraw();
break;
case 'C':
case 'c': ob.Deposit();
break;
case 'D':
case 'd': ob.Display();
break;
case 'Q':
case 'q': ob.ShowInfo();
exit(1);
break;
}
} while (1);
}
int BankAccount::Menu(){
char ch;
cout << "Select an option: ";
cin >> ch;
return ch;
}
简单的答案是:您只有一个银行账户。
如果您查看您的主要功能,您将创建一个,并且只有一个银行帐户。正如您可能猜到的那样,一个银行账户不能用于代表多个银行账户(SoAs 除外)。
因此,您需要一系列银行账户。它是这样的:
BankAccount obs[10];
现在您有 10 个银行账户,没有更多了。因此,每次要创建新的银行帐户时,只需确保在当前未使用的数组中的银行帐户上创建它。
obs[0].CreateAccount();
obs[0].ShowInfo();
obs[0].Deposit();
//Make a new account
obs[1].CreateAccount();
...
为了更进一步,让我们考虑这样一个事实,即您有 10 个银行账户,而只有 10 个。现在这不是很广泛吗?只有 10 个账户的银行是什么?可能在适当的时候停业了。
天真的解决方案是添加更多。 50、100,甚至 1000。但是你真的想每次都回去更新这个数字吗?这太乏味了。
幸运的是,我们可以使用一个方便的容器:
#include <vector>
...
std::vector<BankAccount> obs;
...
这基本上是一个在需要时自动扩展的数组。我不会详细介绍如何使用向量,因为我相信你自己可以很容易地学会这样做。但是,我将为您提供此链接,以便您知道可以从哪里开始。
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 在C应用程序中运行C++(带有STL)函数
- 使用VerQueryValue检索应用程序的文件描述
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用调试/崩溃报告将应用程序部署到客户端
- C++控制台应用程序阻止退出
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 将应用程序从32位移植到64位时出现问题
- 如何改变c++应用程序的视觉效果
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- phytec phyBOARD iMX-6在从闪存而不是SD卡运行qt5 opengles应用程序时表现不佳(FPS减半
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 如何从Windows应用程序输出到标准?
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?