0

給与プロジェクトで問題が発生しています。fstreamを使用して、2つの別々のファイルの書き込みと読み取りを行っています。レコードの追加を実行し、メインメニューの質問に「いいえ」と答えると、プログラムはコード0で終了しますが、ファイルには何も追加されません。「はい」と言ってメインメニューに戻り、別のレコードを追加しようとすると、employee-details.datファイルの失敗の終了コードであるコード5で終了します。両方のファイルはディレクトリにあり、いくつかの変更を行って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

関数voidopenfiles()で、

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 に答える