0

我的工资单项目有问题。我们正在使用 fstream 来写入和读取 2 个单独的文件。每当我们运行添加记录并对主菜单问题回答“否”时,程序以代码 0 退出,但没有任何内容添加到文件中。如果我们说“是”返回主菜单并尝试添加另一条记录,我们会以代码 5 退出,这是employee-details.dat 文件中失败的退出代码。这两个文件都在目录中,并且在我们进行一些修改并开始使用 fstream 之前已经工作。

关于采取哪个方向的任何提示?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;

class payroll
{
private:
    char empnum[10];
    string empfirstname;
    string emplastname;
    char empsin[9];
    char empdob[10];
    char empphone[15];
    string empstreet;
    string empcity;
    string empprovince;
    string empcountry;
    string empstatus;       //Employee status. EX: Terminated, On leave, Active etc.
    double empsalaryei;
    double empsalaryfedtax;
    double empsalarycpp;
    double empsalarynet;
    double empsalarygross;
    double empsalaryprovtax;

public: 
    void addrec(void);
    void modrec(void);
    void viewrec(void);
    void exit1(void);
};

payroll rec;
payroll emp;
bool check = false;
bool filecheck1 = false;
bool filecheck2 = false;
fstream fileemp;
fstream filesal;


void mainmenu()
{

    system("CLS");
    char ch;

    do
    {
        if (check == true)
        {
            system("CLS");
            ch=0;
        }
        else
        {
            cout<<"1. Add an employee\n";
            cout<<"2. Modify employee record\n";
            cout<<"3. View employee record\n";
            cout<<"0. Exit\n";
            cout<<"Please choose an item: ";
            cin>>ch;
        }

        switch(ch)
        {
            case '1':
                emp.addrec();
                break;

            case '2':
                emp.modrec();
                break;

            case '3':
                emp.viewrec();
                break;

            case '0':
                emp.exit1();

        }
    }while(ch !=0);

}

int main()
{
    if (check==false)
    {
        mainmenu();
    }else{
        exit(25);
    }
}

void openfiles()
{
    string filename1 = "employee-details1.dat";
    string filename2 = "payroll-info.dat";

    fileemp.open(filename1.c_str(), ios::in|ios::out|ios::binary);
    filecheck1 = true;
    if(fileemp.fail())
    {
        cout <<"\nSorry the Employee file was not opened"<< "\nPlease check that the file exists" << endl;
        exit(5);
    }

    filesal.open(filename2.c_str(), ios::in|ios::out|ios::binary);
    filecheck2 = true;
    if(filesal.fail())
    {
        cout << "\nSorry the Salary file was not opened"<< "\nPlease check that the file exists" << endl;
        exit(10);
    }
}



void payroll::addrec(void)          //Record adding
{
    system("CLS");
    char userinputadd = ' ';
    check = false;

    if ((filecheck1 == false) && (filecheck2 == false))     //check if our files are open
    {
        openfiles();            //open both of our data files before the user chooses
    }                   

    fileemp.seekp(0L,ios::end);     //go to the end of the employee data file
    filesal.seekp(0L,ios::end);     //go to the end of the salary file
    cout << "Please Enter the Employee Number: ";
    cin>>rec.empnum;
    cin.clear();
    cout << "\nPlease Enter the Employee's First Name: ";
    cin>>rec.empfirstname;
    cin.clear();
    cout << "\nPlease Enter the Employee's Last Name: ";
    cin>>rec.emplastname;
    cin.clear();
    cout << "\nPlease Enter the Employee's Date of Birth (mmddyyyy): ";
    cin>>rec.empdob;
    cin.clear();
    cout << "\nPlease Enter the Employee's Social Insurance Number: ";
    cin>>rec.empsin;
    cin.clear();
    cout << "\nPlease Enter the Employee's Phone Number: ";
    cin>>rec.empphone;
    cin.clear();
    cout << "\nPlease Enter the Employee's Address: ";
    cin>>rec.empstreet;
    cin.clear();
    cout << "\nPlease Enter the Employee's City: ";
    cin>>rec.empcity;
    cin.clear();
    cout << "\nPlease Enter the Employee's Province: ";
    cin>>rec.empprovince;
    cin.clear();
    cout << "\nPlease Enter the Employee's Country: ";
    cin>>rec.empcountry;
    cin.clear();
    cout<<"\nPlease Enter the Employee's Status: ";
    cin>>rec.empstatus;
    cin.clear();
    cout << "\nPlease Enter the Employee's Weekly Gross Salary: ";
    cin >> rec.empsalarygross;
    cin.clear();

    rec.empsalaryei = rec.empsalarygross * 0.0178;
    rec.empsalarycpp = (rec.empsalarygross-134.61)*0.0495;
    rec.empsalaryfedtax = rec.empsalarygross * 0.15;
    rec.empsalaryprovtax = rec.empsalarygross*0.0505;
    rec.empsalarynet = rec.empsalarygross-rec.empsalaryei-rec.empsalarycpp-rec.empsalaryfedtax-rec.empsalaryprovtax;



    fileemp<<rec.empnum<<","<<rec.empfirstname<<","<<rec.emplastname<<","<<rec.empdob<<","<<rec.empsin<<","<<rec.empphone<<","<<rec.empstreet<<","<<rec.empcity<<","<<rec.empprovince<<","<<rec.empcountry<<"0L";
    filesal<<rec.empnum<<","<<rec.empsalaryei<<","<<rec.empsalarycpp<<","<<rec.empsalaryfedtax<<","<<rec.empsalaryprovtax<<","<<rec.empsalarynet<<"0L";
    cin.clear();

    cout<<"Press any key to the main menu.... ";
    getch();
    mainmenu();
}

void payroll::modrec(void)          //Record Modification
{
    system("CLS");
    int empmodnum=0;
    check = false;
    char userinputmod = ' ';

    cout<<"\nEnter the employee number for the record you would like to modify: ";
    cin>>empmodnum;


    cout<<"Press any key to the main menu.... ";
    getch();
    mainmenu();
}

void payroll::viewrec(void)         //Record viewing
{
    int ch1 = 0;
    int i=0;
    system("CLS");
    check = false;
    char userinputview = ' ';
    if ((filecheck1 == false) && (filecheck2 == false))
    {
        openfiles();            //open both of our data files before the user chooses
    }

    cout <<"Which file would you like to view?"
    <<"\n1) Employee Details"
    <<"\n2) Employee Salary Info\n";
    cin >> ch1;

    if(ch1==1)
    {
        i=0;
        fileemp.seekg(0L,ios::beg);
        cout<<"\nList of Records in the Employee Data file"<<endl<<endl;


        while(fileemp.read((char*)&rec,sizeof(rec)))
        {
            cout<<endl<<"Record#"<<""<<i++<<setw(4)<<rec.empnum<<setw(10)
            <<rec.empfirstname<<setw(20)<<rec.emplastname<<setw(30)
            <<rec.empsin<<setw(9)<<rec.empdob<<setw(10)<<rec.empphone<<setw(15)
            <<rec.empstreet<<setw(25)<<rec.empcity<<setw(15)<<rec.empprovince<<setw(15)
            <<rec.empcountry<<setw(15)<<rec.empstatus<<setw(10)
            <<endl;
        }

        if (i==0)
        {
            cin.clear();
            cout <<"\nNO RECORDS EXIST";
            cout <<"\nPress any key to continue...";
            getch();
        }else{
            cin.clear();
            cout<<endl<<"Press any key...";
            getch();
        }
    }else if(ch1==2)
    {
        i=0;
        filesal.seekg(0L,ios::beg);
        cout <<"\nList of Records in the Employee Salary file"<<endl<<endl;

        while(filesal.read((char*)&rec,sizeof(rec)))
        {
            cout <<endl<<"Record#"<<""<<i++<<setw(4)
            <<rec.empnum<<setw(10)<<rec.empsalarygross<<setw(15)
            <<rec.empsalaryei<<setw(10)<<rec.empsalarycpp<<setw(10)
            <<rec.empsalaryfedtax<<setw(10)<<rec.empsalaryprovtax<<setw(10)
            <<rec.empsalarynet<<setw(10)
            <<endl;
        }
        if (i==0)
        {
            cout <<"\nNO RECORDS EXIST";
            cout <<"\nPress any key to continue...";
            getch();
            mainmenu();
        }else{
            cout<<endl<<"Press any key...";
            getch();
            mainmenu();
        }
    }
}

void payroll::exit1(void)
{
    fileemp.close();
    filesal.close();
    filecheck1 = false;
    filecheck2 = false;
    check = true;
}
4

1 回答 1

2

在函数void openfiles()中,您使用过

fileemp.open(filename1.c_str(), ios::in|ios::out|ios::binary||ios::app);
.
.
.
.
filesal.open(filename2.c_str(), ios::in|ios::out|ios::binary||ios::app);

它应该是

fileemp.open(filename1.c_str(), ios::in|ios::out|ios::binary|ios::app);
.
.
.
.
filesal.open(filename2.c_str(), ios::in|ios::out|ios::binary|ios::app);

在此更改之后,我对其进行了编译,它似乎工作正常。

于 2012-04-12T19:03:28.640 回答