账户管理系统,求指教
下面的程序能运行 就是不能进行插入操作求指教#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
class Account{
public:
Account (int = 0, char* = "", double = 0.0);
void setAccountNum(int); //设置账号
int getAccountNum(); //取得账号
void setName(char*); //设置姓名
char* getName(); //取得姓名
void setBal(double); //设置余额
double getBal(); //取得余额
private:
int accountNum; //用户账号
char name[20]; //用户姓名
double balance; //余额
};
Account::Account(int newAccNum, char newName[], double newBal)
{
accountNum = newAccNum;
strcpy(name, newName);
balance = newBal;
}
void Account::setAccountNum(int newAccNum)
{
accountNum = newAccNum;
}
int Account::getAccountNum()
{
return accountNum;
}
void Account::setName(char newName[])
{
strcpy(name, newName);
}
char* Account::getName()
{
return name;
}
void Account::setBal(double newBal)
{
balance = newBal;
}
double Account::getBal()
{
return balance;
}
class AccountOper{
public:
AccountOper();
void createFile(); //创建账户文件,并将相关用户数据输入
int funcChoice(); // 选择功能按钮
void printRec(); //显示
void updateRec(); //更新
void insertRec(); // 插入
void deleteRec(); // 删除
void inquirerec(); //查询
void dispLine(); //显示一行
bool checkAccount(); //检查账户的有效性
void accexit(); //退出
private:
Account acc; //用来临时存放一个用户的账户信息
fstream inout; // 用来对输入输出文件进行操作
char filename[81]; // 用来存放一个指定的文件名
};
AccountOper::AccountOper()
{
cout << "Please input create File: " << endl;
cin.getline(filename, 80);
inout.clear();
inout.open(filename, ios::binary|ios::out|ios::app);
if(inout.fail()){
cout << "The create fail!" << endl;
exit(1);
}
cout << "Account file: " << filename << "create succee, you could go on!" << endl;
inout.close();
}
void AccountOper::createFile()
{
int accNum;
char name[20];
double bal;
inout.clear();
inout.open(filename, ios::binary|ios::out|ios::app);
if (inout.fail()){
cout << "The create fail!" << endl;
exit(1);
}
cout << "Please input account Num(1~200): ";
cin >> accNum;
acc.setAccountNum(accNum);
while (checkAccount()){
cout << "Please input Account name: ";
cin >> name;
acc.setName(name);
cout << "Please input Account balance: ";
cin >> bal;
acc.setBal(bal);
inout.seekp((acc.getAccountNum()-1)*sizeof(Account),ios::beg);
inout.write((char*)&acc,sizeof(Account));
cout << "Please input account Num(1~200): ";
cin >> accNum;
acc.setAccountNum(accNum);
}
inout.close();
}
int AccountOper::funcChoice()
{
cout << "==================================================" << endl;
cout << " Please input function Number:(1,2,3,4,5,6,0) " << endl;
cout << " 1.Please input account to binary file " << filename << endl;
cout << " 2.Show file content " << endl;
cout << " 3.Update account message " << endl;
cout << " 4.Insert account message " << endl;
cout << " 5.Delete account message " << endl;
cout << " 6.Iquirerec account message " << endl;
cout << " 0.Exit " << endl;
cout << "==================================================" << endl;
int choice;
cout << "Please input need function Number: ";
cin >> choice;
return choice;
}
void AccountOper::printRec()
{
inout.clear();
inout.open(filename, ios::binary|ios::in);
if(inout.fail()){
cout << "Open file fail!" << endl;
exit(1);
}
cout << left << setw(10) << "Account Number";
cout << setw(16) << "Account Name";
cout << setw(11) << setw(10) << "Account Balance" << endl;
inout.seekg(0, ios::beg);
inout.read((char*)&acc, sizeof(Account));
while(!inout.eof()){
if (acc.getAccountNum()!=0)
dispLine();
inout.read((char*)&acc, sizeof(Account));
}
inout.close();
}
void AccountOper::updateRec()
{
int accNum;
cout << "Please input need update Number: ";
cin >> accNum;
inout.clear();
inout.open(filename, ios::binary|ios::in|ios::out);
if (inout.fail()){
cout << "Open file fail!" << endl;
exit(1);
}
inout.seekg((accNum-1)*sizeof(Account), ios::beg);
inout.read((char*)&acc, sizeof(Account));
if (acc.getAccountNum()==accNum){
cout << "The need update content: " << endl;
dispLine();
cout << "input deposit(+), take out(-): " << endl;
double money;
cin >> money;
acc.setBal(money + acc.getBal());
cout << "update message content:" << endl;
dispLine();
inout.seekp((accNum-1)*sizeof(Account), ios::beg);
inout.write((char*)&acc, sizeof(Account));
}
else
cout << "Sorry, No have update message!" << endl;
inout.close();
}
void AccountOper::insertRec()
{
int accNum;
char name[20];
double bal;
cout << "Please input account Number: ";
cin >> accNum;
inout.clear();
inout.open(filename, ios::binary|ios::in|ios::out);
if (inout.fail()){
cout << "Open account fail!" << endl;
exit(1);
}
inout.seekg((accNum-1)*sizeof(Account), ios::beg);
inout.read((char*)&acc, sizeof(Account));
if (acc.getAccountNum()==accNum)
cout << "Sorry, the file exist!" <<endl;
else{
acc.setAccountNum(accNum);
cout << "Please input account name: ";
cin >> name;
acc.setName(name);
cout << "Please input balance: ";
cin >> bal;
acc.setBal(bal);
inout.seekp((accNum-1)*sizeof(Account), ios::beg);
inout.write((char*)&acc, sizeof(Account));
}
inout.close();
}
void AccountOper::deleteRec()
{
int accNum;
cout << "Please input delete Number: ";
cin >> accNum;
inout.clear();
inout.open(filename, ios::binary|ios::in|ios::out);
if (inout.fail()){
cout << "The file open fail!" << endl;
exit(1);
}
inout.seekg((accNum-1)*sizeof(Account), ios::beg);
inout.read((char*)&acc, sizeof(Account));
if (acc.getAccountNum()==accNum){
cout << "It's will delete file content:" << endl;
dispLine();
cout << "Input black message in this message" << endl;
Account account;
inout.seekp((accNum-1)*sizeof(Account), ios::beg);
inout.write((char*)&account, sizeof(Account));
}
else
cout << "Sorry, No delete file message!" << endl;
inout.close();
}
void AccountOper::inquirerec()
{
int accNum;
cout << "Please input you will need inquirerec Number: ";
cin >> accNum;
inout.clear();
inout.open(filename, ios::binary|ios::in);
if (inout.fail()){
cout << "The file open fail!" << endl;
exit(1);
}
inout.seekg((accNum-1)*sizeof(Account), ios::beg);
inout.read((char*)&acc, sizeof(Account));
if (acc.getAccountNum()==accNum){
cout << "The content of inquirerec!" << endl;
dispLine();
}
else
cout << "Sorry, no inquirerec message!" << endl;
inout.close();
}
void AccountOper::accexit()
{
cout << "====================================================" << endl;
cout << " Thank you apply " << endl;
cout << " welcome to this " << endl;
cout << " Bye-Bye " << endl;
cout << "====================================================" << endl;
}
void AccountOper::dispLine()
{
cout << left <<setw(10) << acc.getAccountNum();
cout << setw(16) << acc.getName();
cout << setw(11) << setw(10) << acc.getBal() << endl;
}
bool AccountOper::checkAccount()
{
if (acc.getAccountNum()>=1 && acc.getAccountNum()<=200)
return 1;
else
return 0;
}
int main()
{
AccountOper account;
int func;
while(1){
func=account.funcChoice();
switch(func){
case 1: account.createFile(); break;
case 2: account.printRec(); break;
case 3: account.updateRec(); break;
case 4: account.insertRec(); break;
case 5: account.deleteRec(); break;
case 6: account.inquirerec(); break;
case 0: account.accexit(); break;
}
if (!func)
break;
}
}