Untitled

 avatar
unknown
plain_text
2 years ago
9.3 kB
7
Indexable
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

struct employee
{
    int id;
    int age, experience;
    string name, nic;
};

employee emp[10];
int index = 0;

void enter();
void view();
void displayshortlisted();
void search();
void update();
void del();
void fileview();
bool full();
bool empty();

bool full()
{
    if (index == 10)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool empty()
{
    if (index == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    cout << "*******WELCOME TO EMPLOYEE RECOMMENDATION SYSTEM*********" << endl;
    int choice;
    char cont;
    cout << "Press f or F to enter the program." << endl;
    cin >> cont;
    while (cont == 'f' || cont == 'F')
    {
        cout << "Press 1 for entering data of employee. " << endl;
        cout << "Press 2 to view the data of all the employees entered. " << endl;
        cout << "Press 3 to search data on any employee. " << endl;
        cout << "Press 4 to update data of any employee. " << endl;
        cout << "Press 5 to view shortlisted employees. " << endl;
        cout << "Press 6 to delete record of any employee. " << endl;
        cout << "Give your choice below." << endl;
        cin >> choice;

        if (choice == 1)
        {
            enter();
        }
        else if (choice == 2)
        {
            view();
        }
        else if (choice == 3)
        {
            search();
        }
        else if (choice == 4)
        {
            update();
        }
        else if (choice == 5)
        {
            displayshortlisted();
        }
        else if (choice == 6)
        {
            del();
        }
        else
        {
            cout << "Invalid input!" << endl;
        }
        fileview();
        cout << "\nIf you want to continue with the program press 'f' or 'F'. Else press any other key to exit the program." << endl;
        cin >> cont;
    }

    return 0;
}

void enter()
{
    if (index == 10)
    {
        cout << "Array is already full." << endl;
    }
    else
    {   
        emp[index].id = index;
        cout << "ID :" << emp[index].id << endl;
        cout << "Enter employee name : ";
        cin.ignore();
        getline(cin, emp[index].name);

        cout << "Enter age of employee : ";
        cin >> emp[index].age;

        cout << "Enter CNIC number of employee : ";
        cin.ignore();
        getline(cin, emp[index].nic);

        for (int j = 0; j < index; j++)
        {
            if (emp[j].nic == emp[index].nic)
            {
                cout << "CNIC should be unique of employees." << endl;
                cout << "Enter a valid CNIC number : ";
                getline(cin >> ws, emp[index].nic);
                j = -1;
            }
        }

        cout << "Enter experience of employee in (Years) : ";
        cin >> emp[index].experience;

        index++;

    }
}

void view()
{
    if (index == 0)
    {
        cout << "No data entered to display." << endl;
    }
    else
    {
        system("cls");
        for (int i = 0; i < index; i++)
        {
            cout << "\nEmployee ID :" << emp[i].id;
            cout << "\nEmployee name :" << emp[i].name;
            cout << "\nEmployee age :" << emp[i].age;
            cout << "\nEmplyee CNIC no. :" << emp[i].nic;
            cout << "\nEMployee experience :" << emp[i].experience;

        }
        fstream value;
        value.open("fawaz.txt", ios::out);
        value << "Employee ID : " << emp[index].id << endl;
        value << "Employye's Name : " << emp[index].name << endl;
        value << "Employee's age : " << emp[index].age << endl;
        value << "Emplyee's CNIC no. : " << emp[index].nic << endl;
        value << "Emplyee's experience : " << emp[index].experience << endl;
        value.close();
    }
}

void search()
{
    if (index == 0)
    {
        cout << "No data available to search from" << endl;
    }
    else
    {
        system("cls");
        int z;
        cout << "Enter ID you want to search :";
        cin >> z;
        bool found = false;
        for (int i = 0; i < index; i++)
        {
            if (emp[i].id == z)
            {
                found = true;
                cout << "ID found!" << endl;
                cout << "\nEmployee ID :" <<emp[i].id;
                cout << "\nEmployee name :" << emp[i].name;
                cout << "\nEmployee age :" << emp[i].age;
                cout << "\nEmplyee CNIC no. :" << emp[i].nic;
                cout << "\nEMployee experience :" << emp[i].experience;
            }
        }
        if (found = false)
        {
            cout << "ID not found." << endl;
        }

    }
}

void update()
{
    if (index == 0)
    {
        cout << "No records available to update." << endl;
    }
    else
    {
        system("cls");
        int k;
        cout << "Enter ID of the employee whose records you want to update :";
        cin >> k;

        bool found = false;
        for (int i = 0; i < index; i++)
        {
            if (emp[i].id == k)
            {
                cout << "ID found." << endl;
                cout << "Enter new Records.";
                cout << "Employwee ID :";
                cout <<emp[i].id;
                cout << "\nEmployee new name :"; 
                cin >> emp[i].name;
                cout << "\nEmployee new age :";
                cin >> emp[i].age;
                cout << "\nEmplyee new CNIC no. :";
                cin >> emp[i].nic;
                cout << "\nEMployee new experience :";
                cin >> emp[i].experience;
            }
        }

        remove("fawaz.txt");
        fstream remakedata;
        remakedata.open("fawaz.txt", ios::out);
        for (int i = 0; i <= index; i++) {
            remakedata << "Employee ID : " << emp[index].id << endl;
            remakedata << "Employye's Name : " << emp[index].name << endl;
            remakedata << "Employee's age : " << emp[index].age << endl;
            remakedata << "Emplyee's CNIC no. : " << emp[index].nic << endl;
            remakedata << "Emplyee's experience : " << emp[index].experience << endl;
        }
        remakedata.close();
    }
}

void displayshortlisted()
{
    if (index == -1)
    {
        cout << "No records to display." << endl;
    }
    else
    {
        system("cls");
        for (int i = 0; i < index; i++)
        {
            if (emp[i].age <= 35 && emp[i].experience >= 5)
            {
                cout << "********SHORT LISTED EMPLOYEES**********" << endl;
                cout << "Employee ID : " << emp[i].id << endl;
                cout << "Employee name : " << emp[i].name << endl;
                cout << "Employee age : " << emp[i].age << endl;
                cout << "Employee CNIC: " << emp[i].nic << endl;
                cout << "Employee experience : " << emp[i].experience << endl;
            }
            else {
                cout << " \nThe employee " << emp[i].id << " Does not fall on the shortlisted employee criteria." << endl;
            }
        }

    }
}

void del()
{
    if (index == 0)
    {
        cout << "No records available to delete from." << endl;
    }
    else
    {
        system("cls");
        cout << "***************Delete record of any Employee***************" << endl;
        int x;
        int j{};
        bool found = false;
        cout << "Enter ID of the employee whose record you want to delete : ";
        cin >> x;

        for (int i = 0; i < index; i++)
        {
            if (x == emp[i].id)
            {
                found = true;
                cout << "ID FOUND!" << endl;
                j = x;
                cout << "Record deleted of ID no.:" << x << endl;
            }

            for (int i = j; i < index; i++)
            {
                emp[j].id = emp[j + 1].id;
                emp[j].name = emp[j + 1].name;
                emp[j].age = emp[j + 1].age;
                emp[j].nic = emp[j + 1].nic;
                emp[j].experience = emp[j + 1].experience;
            }
            emp[index].id = NULL;
            emp[index].name = " ";
            emp[index].age = NULL;   //Can't NULL string. Have to us ' " " ' to in place of NULL.
            emp[index].nic = " ";
            emp[index].experience = NULL;
            index--;

            fstream remakeindex;
            remakeindex.open("index.txt", ios::out);
            remakeindex << index;
            remakeindex.close();

            remove("fawaz.txt");
            fstream remakedata;
            remakedata.open("fawaz.txt", ios::out);
            remakedata << "Employee ID : " << emp[index].id << endl;
            remakedata << "Employye's Name : " << emp[index].name << endl;
            remakedata << "Employee's age : " << emp[index].age << endl;
            remakedata << "Emplyee's CNIC no. : " << emp[index].nic << endl;
            remakedata << "Emplyee's experience : " << emp[index].experience << endl;
            remakedata.close();

        }
    }
}

void fileview()
{
    fstream getdata;
    getdata.open("fawaz.txt", ios::in);
    string x;
    while (getline(getdata, x))
    {
        cout << x << endl;
    }
    system("pause");
}
Editor is loading...