2012-04-12 42 views
0

我遇到工资项目问题。我们正在使用fstream来从两个单独的文件中读写。每当我们运行添加记录并对主菜单问题回答no时,程序以代码0退出,但没有任何内容添加到文件中。如果我们说是回到主菜单并尝试添加另一条记录,那么我们退出代码5,这是employee-details.dat文件中失败的退出代码。这两个文件都在目录中,并且在我们做了一些修改并开始使用fstream之前已经工作。工资项目中的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; 
} 
+3

您不允许递归调用main()(任何其他函数都可以,但不是主要的)。但是说一个循环可能是一个更好的主意。 – 2012-04-12 18:03:15

+0

您应该减少此代码;尽可能多地简化它,在每一步都进行测试,并且在仍然重现行为的过程中了解它有多简单。这个错误在这个过程中可能会变得很明显,如果不是的话,你可以发布结果,而且我们还有很多工作要做。 – Beta 2012-04-12 18:04:03

+1

您正在混合C stdio和C++流,这是一件坏事。对'fflush(stdin)'的调用不会对'std :: cin'做任何事情。 – 2012-04-12 18:38:52

回答

2
在功能 无效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); 

此更改后,我编译它,它似乎是工作精细。

+0

对不起,我无法捕捉到不同的nims。 – Dani 2012-04-12 19:26:50

+0

@Dani你写了ios :: binary || ios :: app(“||”) 它应该是ios :: binary | ios :: app(“|”) – nims 2012-04-12 20:01:45

+0

哈哈,这将解释为什么当我删除iOS它的工作:P – Dani 2012-04-12 22:09:16