为什么我的程序每隔运行一次只给我显示结果?

时间:2019-07-27 04:38:28

标签: c++

我已经为计算机科学课编写了一个员工数据库程序。我遇到的问题是,在添加雇员之后,使用display all功能,即使我没有运行任何代码,它也只会在我每次运行程序时每隔一次显示雇员。即使声明“记录添加成功”,也会发生这种情况。搜索创建的员工的记录时也会发生同样的事情。

我尝试过的一些事情包括:在switch语句中切换案例的顺序。在添加功能和插入记录功能中输入相同的信息,然后尝试全部显示。以及在显示功能中移动一些数据。仍然获得相同的50/50结果。我对编程还是比较陌生,所以任何输入都会有很大帮助!谢谢!

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>

using namespace std;

//Employee class Declaration
class Employee{
    private:
        char fieldName[40];
        char first[20];
        char last[20];
        int id;
        int ssNum;
        float salary;
        int age;
    public:
        void read();
        void display();
        //will return employee SS#
        int getEmpSS()            { return id;}
        //will return employee salary
        float getSalary()             { return salary;}
        //will update employee salary
        void updateSalary(float s)  { salary = s;}
};

//Read employee record
void Employee::read(){
    cout << "Enter employee ID: ";
    cin >> id;
    cout << "Enter First name: ";
    cin.ignore(1);
    cin.getline(first,20);
    cout << "Enter Last name: ";
    cin.ignore(1);
    cin.getline(last,20);
    cout << "Enter employee Field Name: ";
    cin.ignore(1);
    cin.getline(fieldName,40);
    cout << "Enter your Social Security number: ";
    cin >> ssNum;
    cout << "Enter salary: $";
    cin >> salary;
    cout << "Enter employee age: ";
    cin >> age;
}

//Display employee record
void Employee::display()
{
    cout << "<< EMPLOYEE INFO >>" << endl;
    cout << "ID: " << id << " " << first << " " << last << endl;
    cout << "Age: " << age << endl;;
    cout << "Social Security Number: " << ssNum << endl;
    cout << "Salary: $" << salary << endl;
    cout << "Field Name: " << fieldName << endl;
}

//global declaration
fstream file;

//Will delete file when program is being executed
//because we created a file in append mode
void deleteExistingFile(){
    remove("employeeData.DAT");
}

//function to append record into file
void appendToFille(){
    Employee x;

    //Read employee record from user
    x.read();

    file.open("employeeData.DAT",ios::binary|ios::app);
    if(!file){
        cout << "ERROR IN CREATING FILE\n";
        return;
    }
    //write into file
    file.write((char*)&x,sizeof(x));
    file.close();
    cout << "Record added sucessfully.\n";
}

void displayAll(){
    Employee x;

    file.open("employeeData.DAT",ios::binary|ios::in);
    if(!file){
        cout << "ERROR IN OPENING FILE \n";
        return;
    }
    while(file){
    if(file.read((char*)&x,sizeof(x)))
        if(x.getSalary()>=10000 && x.getSalary()<=20000)
            x.display();
    }
  file.close();
}

void searchForRecord(){

    //read employee id
    Employee x;
    int c;
    int isFound = 0;

    cout << "Enter employee SS#: ";
    cin >> c;


    file.open("employeeData.DAT",ios::binary|ios::in);
    if(!file){
        cout << "ERROR IN OPENING FILE \n";
        return;
    }
    while(file){
        if(file.read((char*)&x,sizeof(x))){
            if(x.getEmpSS()==c){
                cout << "RECORD FOUND\n";
                x.display();
                isFound = 1;
                break;
            }
        }
    }
    if(isFound == 0){
        cout << "Record not found!\n";
    }
    file.close();
}

//Function to increase salary
void increaseSalary(){
    //read employee id
    Employee x;
    int c;
    int isFound=0;
    float sal;

    cout<<"enter employee code \n";
    cin>>c;


    file.open("employeeData.DAT",ios::binary|ios::in);
    if(!file){
        cout<<"ERROR OPENING FILE \n";
        return;
    }
    while(file){
        if(file.read((char*)&x,sizeof(x))){
            if(x.getEmpSS()==c){
                cout << "Salary Increase: ";
                cin >> sal;
                x.updateSalary(x.getSalary()+sal);
                isFound = 1;
                break;
            }
        }
    }
    if(isFound==0){
        cout<<"Record not found!!!\n";
    }
    file.close();
    cout<<"Salary updated successfully."<<endl;
}

//Insert record by assuming that records are in 
//ascending order
void insertRecord(){
    //read employee record
    Employee    x;
    Employee newEmp;

    //Read record to insert
    newEmp.read();

    fstream fin;
    //read file in input mode
    file.open("employeeData.DAT",ios::binary|ios::in);
    //open file in write mode
    fin.open("TEMP.DAT",ios::binary|ios::out);

    if(!file){
        cout<<"Error in opening employeeData.DAT file!!!\n";
        return;
    }
    if(!fin){
        cout<<"Error in opening TEMP.DAT file!!!\n";
        return;
    }
    while(file){
        if(file.read((char*)&x,sizeof(x))){
            if(x.getEmpSS()>newEmp.getEmpSS()){
                fin.write((char*)&newEmp, sizeof(newEmp));
            }
            //no need to use else
            fin.write((char*)&x, sizeof(x));
        }
    }

    fin.close();
    file.close();

    rename("TEMP.DAT","employeeData.DAT");
    remove("TEMP.DAT");
    cout<<"Record inserted successfully."<<endl;
}

int main()
{
     char ch;

    //if required then only remove the file
     deleteExistingFile();

     do{
     int n;

     cout << "ENTER CHOICE\n" << "1. ADD AN EMPLOYEE\n" << "2. DISPLAY\n" << "3. SEARCH\n" << "4. INCREASE SALARY\n" << "5. INSERT RECORD\n";
     cout << "Make a choice: ";
     cin >> n;

     switch(n){
          case 1:
            appendToFille();
            break;
          case 2 :
            displayAll();
            break;
          case 3:
            searchForRecord();
            break;
        case 4:
            increaseSalary();
            break;
        case 5:
            insertRecord();
            break;

          default :
                cout << "Invalid Choice\n";
     }

     cout << "Do you want to continue? (y/Y): ";
     cin >> ch;

     } while(ch == 'Y'||ch == 'y');

    return 0;
}

0 个答案:

没有答案