该程序用于模拟银行的计算机终端。
我第一次运行程序并选择选项6而没有先打开帐户我得到了正确的显示: 没有活动帐户
然后我重新运行该程序并选择选项1以打开一个新帐户和必要的信息。然后,当我选择选项6时,它会显示:
Baton Rouge Trust Bank
Customers Listing
100 Les Miles Dr., Baton Rouge, Louisiana 70802
===========================================================
a/c # First Name Last Name Balance
-----------------------------------------------------------
1234-56-78 Jane Doe 500
0
===========================================================
从那里开始,每次运行程序时都会出现更糟糕的情况。例如:
Baton Rouge Trust Bank
Customers Listing
100 Les Miles Dr., Baton Rouge, Louisiana 70802
===========================================================
a/c # First Name Last Name Balance
-----------------------------------------------------------
0 0
0 0
1234-56-78 Jane Doe 500
0 0
0
===========================================================
我无法弄清楚是什么导致了这些问题,特别是在第二个程序运行时,应该只有一个银行帐户。
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
const int MAX_CUSTOMERS = 1000;
const double OVER_DRAFT = 35.00;
struct AccountInfo
{
string acNum;
string fName;
string lName;
char midInitial;
double balance;
};
int linearSearch(string acno, const AccountInfo customers[], int numAccs)
{
int i;
for (i=0; i<numAccs; i++)
{
if (customers[i].acNum == acno)
return i;
}
return -1;
}
void openAccount(string acno, string fName, string lName, char midInit, double initBal, AccountInfo customers[], int& numAccs)
{
customers[numAccs].acNum = acno;
customers[numAccs].lName = lName;
customers[numAccs].fName = fName;
customers[numAccs].midInitial = midInit;
customers[numAccs].balance = initBal;
numAccs++;
}
void closeAccount(string acno, AccountInfo customers[], int& numAccs)
{
int row = linearSearch(acno,customers,numAccs);
while (row < numAccs)
{
customers[row] = customers[row+1];
row++;
}
numAccs--;
}
void deposit(string acno, double amount, AccountInfo customers[], int numAccs)
{
int row = linearSearch(acno,customers,numAccs);
customers[row].balance = customers[row].balance + amount;
}
void withdraw(string acno, double amount, AccountInfo customers[], int numAccs)
{
int row = linearSearch(acno,customers,numAccs);
if (amount <= customers[row].balance)
customers[row].balance = customers[row].balance - amount;
else if (amount > customers[row].balance)
customers[row].balance = customers[row].balance - amount - OVER_DRAFT;
}
void inquiry(string acno, const AccountInfo customers[], int numAccs)
{
int row = linearSearch(acno,customers,numAccs);
cout<<"----------------------------------"<<endl;
cout<<"customer: "<<customers[row].fName<<" "<<customers[row].midInitial<<" " <<customers[row].lName<<endl;
cout<<"A/C #: "<<customers[row].acNum<<endl;
cout<<"Balance: $"<<customers[row].balance<<endl;
cout<<"----------------------------------"<<endl;
}
void customersList(const AccountInfo customers[], int numAccs)
{
cout<<right<<setw(40)<<"Baton Rouge Trust Bank"<<endl;
cout<<right<<setw(37)<<"Customers Listing"<<endl;
cout<<right<<setw(52)<<"100 Les Miles Dr., Baton Rouge, Louisiana 70802"<<endl;
cout<<"==========================================================="<<endl;
cout<<left<<setw(17)<<"a/c #"<<setw(18)<<"First Name"<<setw(17)<<"Last Name"<<setw(9) <<"Balance"<<endl;
cout<<"-----------------------------------------------------------"<<endl;
for (int index=0; index<=numAccs; index++)
{
if (numAccs==0)
{
cout<<"***THERE ARE NO ACTIVE ACCOUNTS***"<<endl;
}
else
{
cout<<left<<setw(17)<<customers[index].acNum<<setw(18)<<customers[index].fName<<setw(17)<<customers[index].lName<<setw(9)<<customers[index].balance<<endl;
}
}
cout<<"==========================================================="<<endl;
}
void menu()
{
cout<<endl<<endl;
cout<<" Baton Rouge Trust ATM "<<endl;
cout<<"========================"<<endl;
cout<<"OPEN ACCOUNT..........[1]"<<endl;
cout<<"CLOSE AN ACCOUNT......[2]"<<endl;
cout<<"DEPOSIT...............[3]"<<endl;
cout<<"WITHDRAW..............[4]"<<endl;
cout<<"BALANCE INQUIRY.......[5]"<<endl;
cout<<"CUSTOMER LISTING......[6]"<<endl;
cout<<"EXIT..................[0]"<<endl;
}
int main()
{
AccountInfo customers[MAX_CUSTOMERS];
string acno, lName, fName;
double initBal, amount;
char midInit;
int numAccs=0, pos, option;
fstream acInfoIn, acInfoOut;
acInfoIn.open("acinfo.dbf",ios::in);
numAccs = 0;
while (numAccs <MAX_CUSTOMERS && acInfoIn>>customers[numAccs].acNum)
{
acInfoIn>>customers[numAccs].lName>>customers[numAccs].fName
>>customers[numAccs].midInitial>>customers[numAccs].balance;
numAccs++;
}
acInfoIn.close();
do
{
menu();
cout<<endl;
cout<<"Select an option->";
cin>>option;
cout<<endl;
switch(option)
{
case 1:
cout<<"Enter a 10-character long account number->";
cin>>acno;
if (acno.length() != 10)
{
cout<<acno<<" must be 10-character long."<<endl;
}
else if (linearSearch(acno,customers,numAccs) > 0)
{
cout<<acno<<" cannot be assigned to multiple customers."<<endl;
}
else
{
cout<<"Customer's first name ->";;
cin>>fName;
cout<<"Customer's middle initial ->";
cin>>midInit;
cout<<"Customer's last name ->";
cin>>lName;
cout<<"Initial Deposit ->";
cin>>initBal;
if (initBal < 25.00)
cout<<"The initial balance required for a new account must be at least $25.00."<<endl;
else
openAccount(acno,fName,lName,midInit,initBal,customers,numAccs);
}
break;
case 2:
cout<<"Enter a 10-character long account number->";
cin>>acno;
if (acno.length() != 10)
cout<<acno<<" must be 10 characters long."<<endl;
else if (linearSearch(acno,customers,numAccs) < 0)
cout<<acno<<" is not a valid account number."<<endl;
else
{
int pos = linearSearch(acno,customers,numAccs);
if (customers[pos].balance != 0)
cout<<"This account still has money in its account; thus it cannot be closed."<<endl;
else
closeAccount(acno,customers,numAccs);
}
break;
case 3:
cout<<"Enter a 10-character long account number->";
cin>>acno;
if (acno.length() != 10)
{
cout<<acno<<" must be 10-character long."<<endl;
}
else if (linearSearch(acno,customers,numAccs) < 0)
{
cout<<acno<<" is not a valid account."<<endl;
}
else
{
cout<<"Enter the amount to be deposited-> ";
cin>>amount;
if (amount <= 0)
cout<<"The amount to be deposited must be at least a penny."<<endl;
else
deposit(acno,amount,customers,numAccs);
}
break;
case 4:
cout<<"Enter a 10-charcter long account number->";
cin>>acno;
if (acno.length() != 10)
cout<<acno<<" must be 10 characters long."<<endl;
else if (linearSearch(acno,customers,numAccs) < 0)
cout<<acno<<" is not a valid account number."<<endl;
else
{
cout<<"Enter amount withdrawn->";
cin>>amount;
if (amount <= 0)
cout<<"This amount is invalid. The amount withdrawn must at least be a penny.";
else
withdraw(acno,amount,customers,numAccs);
}
break;
case 5:
cout<<"Enter a 10-charcter long account number->";
cin>>acno;
if (acno.length() != 10)
cout<<acno<<" must be 10 characters long."<<endl;
else if (linearSearch(acno,customers,numAccs) < 0)
cout<<acno<<" is not a valid account number."<<endl;
else
inquiry(acno,customers,numAccs);
break;
case 6:
customersList(customers, numAccs);
break;
case 0:
break;
default:
cout<<"Invalid menu option"<<endl;
}
}while (option != 0);
acInfoOut.open("acinfo.dbf",ios::out);
for (pos=0; pos < MAX_CUSTOMERS; pos++)
{
acInfoOut<<customers[pos].acNum<<endl;
acInfoOut<<customers[pos].lName<<endl;
acInfoOut<<customers[pos].fName<<endl;
acInfoOut<<customers[pos].midInitial<<endl;
acInfoOut<<customers[pos].balance<<endl;
}
acInfoOut.close();
return 0;
}
答案 0 :(得分:1)
我不喜欢这个
acInfoOut.open("acinfo.dbf",ios::out);
for (pos=0; pos < MAX_CUSTOMERS; pos++)
{
...
}
acInfoOut.close();
我认为你应该这样做
acInfoOut.open("acinfo.dbf",ios::out);
for (pos=0; pos < numAccs; pos++)
{
...
}
acInfoOut.close();
即。将输出限制为您实际拥有的帐户数。