我的程序当前存在两个主要问题。第一个是我无法在程序运行时向其添加一个以上的帐户(我需要关闭该帐户并重新打开,然后才能添加另一个帐户)。第二个问题是,当我不向程序中添加任何帐户时,地址就会保存到程序中,这就是我不向程序中添加任何帐户时文件的外观。
123#John Smith#0#0###-1.07374e+008#-1.07374e+008#
文件的第一部分是正确的,但是地址来自内存中的其他位置。这就是我的代码的样子。
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
struct account
{
string acctNum;
string name;
float cBal;
float sBal;
};
int menu();
char subMenu();
int loadCustomers(account[]);
void saveCusomers(account[], int);
int newCustomer(account[], int);
int deleteCustomer(account[], int);
int findCustomer(account[], int);
void deposit(account[], int);
void withdrawl(account[], int);
void balance(account[], int);
void bankBalance(account[], int);
int main()
{
account acc[20];
int selection;
int numAcc = 0;
int search;
numAcc = loadCustomers(acc);
do
{
selection = menu();
if(selection == 1)
{
newCustomer(acc, numAcc);
}
else if(selection == 2)
{
deleteCustomer(acc, numAcc);
}
else if(selection == 3)
{
search = findCustomer(acc, numAcc);
if (search == -1)
{
cout << "That account doesn't exist." << endl;
system("pause");
system("cls");
}
else
{
cout << right << setw(3) << acc[search].acctNum << "" << left << setw(15) << acc[search].name << acc[search].cBal << acc[search].sBal << endl;
system("pause");
system("cls");
}
}
else if(selection == 4)
{
deposit(acc, numAcc);
}
else if(selection == 5)
{
withdrawl(acc, numAcc);
}
else if(selection == 6)
{
balance(acc, numAcc);
}
else if(selection == 7)
{
bankBalance(acc, numAcc);
}
else if(selection == 8)
{
break;
}
} while (selection != 8);
saveCusomers(acc, numAcc);
return 0;
}
int menu()
{
int select;
cout << "Main Menu" << endl;
cout << "=============" << endl;
cout << "1. New Account" << endl;
cout << "2. Delete Account" << endl;
cout << "3. Find Customer" << endl;
cout << "4. Deposit" << endl;
cout << "5. Withdrawl" << endl;
cout << "6. Balance" << endl;
cout << "7. Bank Balance" << endl;
cout << "8. Exit" << endl;
cout << "=============" << endl;
cout << "Enter choice: ";
cin >> select;
while (select < 1 || select > 8)
{
cout << "Invalid input, select a number between 1 and 8: ";
cin >> select;
}
system("cls");
return select;
}
char subMenu()
{
char choice;
cout << "Which account? <C>hecking or <S>aving: ";
cin >> choice;
while(choice != 'C' && choice != 'c' && choice != 'S' && choice != 's')
{
cout << "Invalid choice, choose either checking or saving: ";
cin >> choice;
}
return choice;
}
int loadCustomers(account acc[])
{
ifstream inFile;
int numCustomers = 0, i = 0;
string ctemp, stemp;
inFile.open("customer.dat");
if (!inFile)
{
cout << "No customer file found." << endl;
}
else
{
cout << "Customer file found..." << endl << endl;
while (getline(inFile, acc[i].acctNum, '#'))
{
getline(inFile, acc[i].name, '#');
getline(inFile, ctemp, '#');
getline(inFile, stemp, '#');
istringstream(ctemp) >> acc[i].cBal;
istringstream(stemp) >> acc[i].sBal;
i++;
numCustomers++;
}
cout << "Number of customers found in file: " << numCustomers << endl;
}
system("pause");
system("cls");
inFile.close();
return numCustomers;
}
void saveCusomers(account acc[], int numCustomers)
{
ofstream outFile;
outFile.open("customer.dat");
for (int i = 0; i < numCustomers; i++)
{
outFile << acc[i].acctNum;
outFile << '#';
outFile << acc[i].name;
outFile << '#';
outFile << acc[i].cBal;
outFile << '#';
outFile << acc[i].sBal;
outFile << '#';
}
outFile << acc[numCustomers].acctNum;
outFile << '#';
outFile << acc[numCustomers].name;
outFile << '#';
outFile << acc[numCustomers].cBal;
outFile << '#';
outFile << acc[numCustomers].sBal;
outFile << '#';
cout << numCustomers + 1 << " accounts saved into the file." << endl;
outFile.close();
}
int newCustomer(account acc[], int numCustomers)
{
cout << "New Customer" << endl;
cout << "============" << endl;
cout << "Enter account number: ";
cin >> acc[numCustomers].acctNum;
cout << "Enter name: ";
cin.ignore();
getline(cin, acc[numCustomers].name);
acc[numCustomers].cBal = 0;
acc[numCustomers].sBal = 0;
numCustomers++;
return numCustomers;
}
int deleteCustomer(account[], int)
{
return 0;
}
int findCustomer(account acc[], int numCustomers)
{
string target;
cout << "Enter the account number you are looking for: ";
cin >> target;
for (int i = 0; i < numCustomers; i++)
{
if (acc[i].acctNum == target)
{
return i;
}
}
return -1;
}
如何更改该名称以使其能够在程序运行时添加多个帐户,以及如何使其不添加任何地址而不会在文件中保存地址?我也想知道为什么那样保存这些地址。
在main()
你loadCustomers()
在acc[]
和你保持客户的局部变量的数量的记录numAcc
。在结束main()
你saveCustomers()
的numAcc
在acc[]
。
1.您的保存功能错误:
在循环中for (int i = 0; i < numCustomers; i++) { ...}
,您首先将每个客户从0写入numAcc-1
。这是对的。
但是之后,您再次写了一个附加帐户,其偏移量为numAcc
。因此,您所写的帐户比以前多了。并且该帐户的值未初始化,这解释了您看到的怪异数字。
即使没有添加任何帐户,您也可以这样做。而且您知道您正在这样做,因为您已经写过cout << numCustomers + 1 << " accounts saved into the file."
更正:只需将您拥有的帐户保存在循环中即可。
2.您添加的功能是正确的,但是您没有按计划使用它:
当您使用newCustomer()
函数添加客户时,其局部变量将递增,以便它知道还有一个客户。您的函数正确返回此新数字。
不幸的是,在main()中,您对于此返回值不执行任何操作,因此numAcc保持不变。
更正:更正以下语句main()
:
if (selection == 1)
{
numAcc = newCustomer(acc, numAcc); // update the number of accounts
}
3.其他说明:
您无需检查newCustomers()
acc []数组是否已满。如果您添加第21个帐户,则将是段错误!
负载也一样。您无法确定没有人使用文本编辑器在文件中添加行。因此,还有阅读更多行的风险,因为您的文件中有空格。
deleteCustomer()
我想您还没有写任何东西,所以什么也没有发生。认为此功能可以完全更改帐户数newCustomer()
。
由于大多数银行拥有超过20个客户,因此我认为这是学校的一项练习。我不知道是否允许您这样做,但我强烈建议您将替换为vectors
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句