数据文件输入/输出错误
Data File Input/Output Error
此程序用于模拟银行的计算机终端。
当我第一次运行程序并选择选项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;
}
我不喜欢这个
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();
也就是说,将产出限制在你实际拥有的账户数量之内。
相关文章:
- 输出错误,问题是找到总和5000位数字cpp
- 使用复制构造函数的程序输出错误
- 异或字符串加密/解密输出错误
- 在linux上使用g++输出错误,在windows上更正
- 使用递归函数 (c++) 将长字符串转换为整数时输出错误
- ECDSA 密钥对生成输出错误
- fscanf() 输出错误的值
- 输入 1024 后十进制到二进制转换的输出错误?
- 输出错误命令的条件语句
- cuSparse (cusparseDcsrgemm) 中的矩阵乘法输出错误的结果
- 为什么 du -sh 输出错误大小的内存映射文件
- VS2017 的输出错误,但 mingw 有效
- 如何使FFMPEG C 代码不输出错误消息
- 乘以时输出错误.这是我编译器中的错误吗?C
- 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO
- 为什么输出错误崩溃
- C++输入/输出错误
- 尝试用 c++ 制作一个简单的加法器.编译成功,但输出错误
- 当我使用此合并排序代码运行时,输出错误
- 递归功能输出错误的值