CS201 Assignment 3 Solution and Discussion


  • Cyberian's Gold

    Assignment No. 3
    Semester: Fall 2019
    CS201 – Introduction to Programming Total Marks: 20
    Due Date: 15-01-2020

    Instructions
    Please read the following instructions carefully before submitting assignment:
    It should be clear that your assignment will not get any credit if:

    o Assignment is submitted after due date.
    o Submitted assignment does not open or file is corrupt.
    o Assignment is copied (From internet/students).

    Software allowed to develop Assignment

    • Dev C++

    Objectives:
    In this assignment, the students will learn:
    • How to make Class while dealing with real life problems
    • How to associate functions with Class.
    • How to implement switch statement for Class based functions.
    • How to deal with file handling.
    • How to manipulate already created file.

    Assignment Submission Instructions
    You are required to submit only .cpp file on the assignments interface of CS201 at VU-LMS.
    Assignment submitted in any other format will not be accepted and will be graded with zero marks.

    Problem Statement
    Write a console-based employee management system through binary File Handling (In order to observe data security, we use binary file format so that data cannot be read directly from the TXT file) which will perform management actions of Employee by taking input from user by showing following three options:

    1. Press 1 to ADD AN EMPLOYEE.
    2. Press 2 to DISPLAY FILE DATA.
    3. Press 3 to INCREASE EMPLOYEE SALARY.
      After dealing with each option, show prompt to user and continue the program until user press other than ‘y’

    Instructions to write C++ program:

     Use Class Employee to declare all Employee’s belongings.
     Make a EMPLOYEE.TXT file for saving Employee records
     Each time program runs, check and delete EMPLOYEE.TXT file if already exists.
     Switch statement will be implemented to perform multiple conditions and to perform different actions based on the conditions. i.e. Option 1, 2 and 3.
     Write functions to perform tasks given in options.
    Screenshots for Guidance:

    1 Initially when program executes, following screen will show to user to get input from the user.
    25701577-8346-420e-9cc7-b1919d746c36-image.png

    2 If user press any other option then 1 at the first time i.e. without creating any file for Employees, then show error to user.
    3c3a2e34-67c5-4664-a901-538a25232780-image.png

    3 If User press 1, then take Employee credentials from user using prompt statements as follows and save that data in binary file named EMPLOYEE:
    5ed9479d-a069-41ce-876d-0dd1134499e7-image.png

    4 If User Press 2, then show all Employee Records to user by reading data from the file, which is depicted as follows:
    47232660-fab8-4841-ae80-684149d84f89-image.png

    5 If User press 3, then ask Employee code from user and then take amount of salary to be increased and update record of that Employee. depicted as follows:
    5392f488-b92c-41ea-b53d-23c2a7061a4c-image.png

    Good Luck!

    Lectures Covered: This assignment covers Lecture # 15-30.
    Deadline: The deadline to submit your assignment solution is 15-01-2020. Your assignment must be submitted within the due date through VU-LMS. No assignment will be accepted through email after the due date.


  • Cyberian's Gold

    Ideas Solution 2:

    #include<iostream>
    
    #include<fstream>
    
    #include<stdio.h>
    
    
    
    
    using namespace std;
    
    class Employee{
    
        private:
    
            int code;
    
            char name[20];
    
            float salary;
    
        public:
    
            void read();
    
            void display();
    
           
    
            int getEmpCode()            { return code;}
    
           
    
            int getSalary()             { return salary;}
    
           
    
            void updateSalary(float s)  { salary=s;}
    
    };
    
    
    
    
    
    
    
    void Employee::read(){
    
        cout<<"Enter employee code: ";
    
        cin>>code;
    
        cout<<"Enter name: ";
    
        cin.ignore(1);
    
        cin.getline(name,20);
    
        cout<<"Enter salary: ";
    
        cin>>salary;
    
    }
    
    
    
    
    
    
    
    void Employee::display()
    
    {
    
        cout<<code<<" "<<name<<"\t"<<salary<<endl;
    
    }
    
    
    
    
    
    
    
    fstream file;
    
    
    
    
    void deleteExistingFile(){
    
        remove("EMPLOYEE.DAT");
    
    }
    
    
    
    
    void appendToFille(){
    
        Employee    x;
    
        
    
        x.read();
    
        
    
        file.open("EMPLOYEE.DAT",ios::binary|ios::app);
    
        if(!file){
    
            cout<<"ERROR IN CREATING FILE\n";
    
            return;
    
        }
    
        file.write((char*)&x,sizeof(x));
    
        file.close();
    
        cout<<"Record added sucessfully.\n";
    
    }
    
    
    
    
    void displayAll(){
    
        Employee    x;
    
        
    
        file.open("EMPLOYEE.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(){
    
        Employee    x;
    
        int c;
    
        int isFound=0;
    
    
    
    
        cout<<"Enter employee code: ";
    
        cin>>c;
    
    
    
    
    
    
    
        file.open("EMPLOYEE.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.getEmpCode()==c){
    
                    cout<<"RECORD FOUND\n";
    
                    x.display();
    
                    isFound=1;
    
                    break;
    
                }
    
            }
    
        }
    
        if(isFound==0){
    
            cout<<"Record not found!!!\n";
    
        }
    
        file.close();
    
    }
    
    
    
    
    void increaseSalary(){
    
        Employee    x;
    
        int c;
    
        int isFound=0;
    
        float sal;
    
    
    
    
        cout<<"enter employee code \n";
    
        cin>>c;
    
    
    
    
    
    
    
        file.open("EMPLOYEE.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.getEmpCode()==c){
    
                    cout<<"Salary hike? ";
    
                    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;
    
    }
    
    
    
    
    void insertRecord(){
    
        Employee    x;
    
        Employee newEmp;
    
        
    
        newEmp.read();
    
    
    
    
        fstream fin;
    
        file.open("EMPLOYEE.DAT",ios::binary|ios::in);
    
        fin.open("TEMP.DAT",ios::binary|ios::out);
    
    
    
    
        if(!file){
    
            cout<<"Error in opening EMPLOYEE.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.getEmpCode()>newEmp.getEmpCode()){
    
                    fin.write((char*)&newEmp, sizeof(newEmp));
    
                }
    
       
    
                fin.write((char*)&x, sizeof(x));
    
            }
    
        }
    
    
    
    
        fin.close();
    
        file.close();
    
        
    
        rename("TEMP.DAT","EMPLOYEE.DAT");
    
        remove("TEMP.DAT");
    
        cout<<"Record inserted successfully."<<endl;
    
    }
    
    
    
    
    int main()
    
    {
    
         char ch;
    
    
    
    
         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 ? : ";
    
         cin>>ch;
    
    
    
    
         }while(ch=='Y'||ch=='y');
    
        
    
        return 0;
    
    }
    

  • Cyberian's Gold

    Ideas Solution:

    #include&lt;iostream&gt;
    #include&lt;fstream&gt;
    #include&lt;string.h&gt;
    
    using namespace std;
    void addemployee();
    void showemployee();
    
    class employee {
    
    int employee_id;
    string employee_name;
    int employee_salary;
    public:
    employee(){
    
    }
    void setid(int id){
    employee_id=id;
    }
    void setname(string name){
    employee_name=name;
    }
    
    void setsalary(int salary){
    employee_salary=salary;
    }
    int getid(){
    return this-&gt;employee_id;
    }
    string getname(){
    return this-&gt;employee_name;
    }
    int getsalary(){
    return this-&gt;employee_salary;
    }
    };
    employee anEmployee;
    void addemployee(){
    int id;
    cout&lt;&lt;&quot;enter employee ID: &quot;;
    cin&gt;&gt;id;
    anEmployee.setid(id);
    cout&lt;&lt;&quot;enter employee Name: &quot;;
    string name;
    cin&gt;&gt;name;
    anEmployee.setname(name);
    int salry;
    
    cout&lt;&lt;&quot;enter employee Salary: &quot;;
    cin&gt;&gt;salry;
    anEmployee.setsalary(salry);
    ofstream empfile(&quot;employee.txt&quot;,ios::binary|ios::app);
    
    if(!empfile) {
    cout &lt;&lt; &quot;Cannot open file!&quot; &lt;&lt; endl;
    return ;
    }
    empfile.write((char*)&amp;anEmployee, sizeof(employee));
    //cout&lt;&lt;anEmployee.getid()&lt;&lt;&quot; &quot;&lt;&lt;anEmployee.getname()&lt;&lt;&quot;
    &quot;&quot;&quot;&lt;&lt;anEmployee.getsalary();
    empfile.close();
    if(!empfile.good()) {
    cout &lt;&lt; &quot;Error occurred at writing time!&quot; &lt;&lt; endl;
    return ;
    }
    cout &lt;&lt; &quot;Employee Recored added Sucessfully!&quot; &lt;&lt; endl;
    }
    
    void displayRecord() {
    // employee anEmployee;
    ifstream file(&quot;employee.txt&quot;,ios::binary);
    if(!file) {
    
    cout&lt;&lt;&quot;Error in opening file.\n&quot;;
    return;
    } else {
    cout&lt;&lt;&quot;ID&quot;&lt;&lt;&quot; &quot;&lt;&lt;&quot;Name&quot;&lt;&lt;&quot;\t&quot;&lt;&lt;&quot;Salary&quot;&lt;&lt;endl;
    cout&lt;&lt;&quot;=======================================================&quot;&lt;&lt;en
    dl;
    while(file.read((char*)&amp;anEmployee,sizeof(employee))) {
    
    cout&lt;&lt;anEmployee.getid()&lt;&lt;&quot;
    &quot;&lt;&lt;anEmployee.getname()&lt;&lt;&quot;\t&quot;&lt;&lt;anEmployee.getsalary();
    cout&lt;&lt;endl;
    }
    file.close();
    }
    }
    
    // function to show employee data
    void updateSalary() {
    fstream file(&quot;employee.txt&quot;,ios::binary|ios::in|ios::out);
    if(!file) {
    cout&lt;&lt;&quot;Error in opening file.\n&quot;;
    return;
    }
    int eCode,sHike;
    
    cout&lt;&lt;&quot;Enter employee code\n&quot;;
    cin&gt;&gt;eCode;
    cout&lt;&lt;&quot;Salary hike? &quot;;
    cin&gt;&gt;sHike;
    file.seekg(sizeof(anEmployee)*(eCode-1),ios::beg);
    file.read((char*)&amp;anEmployee,sizeof(employee));
    anEmployee.setsalary(sHike+anEmployee.getsalary());
    file.seekp(sizeof(anEmployee)*(eCode-1),ios::beg);
    file.write((char*)&amp;anEmployee,sizeof(employee));
    cout&lt;&lt;&quot;Salary updated successfully.\n&quot;;
    }
    main()
    {
    ifstream file(&quot;employee.txt&quot;);
    if(file) {
    file.close();
    remove(&quot;employee.txt&quot;);
    }
    int programOption;
    while(true){
    
    cout&lt;&lt;&quot;\n\n\n&quot;;
    cout&lt;&lt;&quot;Please select the option Below,.\n&quot;;
    cout&lt;&lt;&quot;Please enter 1 To ADD AN EMPLOYEE.\n&quot;;
    
    cout&lt;&lt;&quot;Please enter 2 To DISPLAY FILE DATA..\n&quot;;
    cout&lt;&lt;&quot;Please enter 3 To INCREASE EMPLOYEE SALARY..\n&quot;;
    cout&lt;&lt;&quot;Please enter 4 To Exit Program..\n&quot;;
    
    cout&lt;&lt;&quot;============================================================
    ======\n&quot;;
    cout&lt;&lt;&quot;Please enter program option :&quot;;
    cin&gt;&gt;programOption;
    
    switch(programOption){
    case 1 :
    cout&lt;&lt;&quot;ADD AN EMPLOYEE DATA.\n&quot;;
    addemployee();
    break;
    case 2 :
    cout&lt;&lt;&quot;Showing Employee Data.\n&quot;;
    displayRecord();
    break;
    case 3 :
    cout&lt;&lt;&quot;Update salary .\n&quot;;
    updateSalary();
    break;
    case 4 :
    cout&lt;&lt;&quot;Shuting Down the Program......\n&quot;;
    
    exit(1);
    break;
    
    }
    }
    }
    

  • Cyberian's


  • Cyberian's Gold

    @zareen
    Here you can Also discuss regarding error and code issue!.
    happy to learn smartly!
    Thanks


  • Cyberian's Gold

    Full Code File Available Soon!
    If you have any Problem regarding code issue you can discuss @cyberian !
    d65e9782-4113-40ce-9a0e-c3bbeb8d7351-image.png7a028af0-68ef-41c8-85fc-7a0d0042bd8a-image.png72c02202-ea67-4ab4-a9db-ee0d5aa40f1f-image.png69a27478-0836-4dcc-a65e-38ae99a1e481-image.png8269fc0e-7c8f-4f1f-936b-dfe5a1f66399-image.png

    6bcae402-bf1e-4d18-861d-cf3cd60cdb37-image.png


  • Cyberian's Gold

    Solution will be available soon!



Quiz 100% Result Quiz 100% Result
| |