Answer to Question #158983 in C++ for Ashes

Question #158983

 Write a C++ program that creates a structure array and store the information of 10

employees. Employee is a structure having employeeName and employeeID and Salary as

attributes. Write a function

void DisplayEmployeesRecord (employee a[ ], int s) that displays the array.

Void StoreToFile (employee a[ ], ofstream f, int s) that store each employee information in

a file in the following format

employeeName1 employeeid1 salary1

employeeName2 employeeid2 salary2

employeeName3 employeeid1 salary3

Void storeToArray(employee a[ ], ifstream f, int s) that reads the file and store data to array


1
Expert's answer
2021-01-27T14:33:50-0500
#include <iostream>
#include <fstream>

class employee {
public:
    std::string employeeName;
    signed int employeeID, Salary;

    employee(std::string employeeName, int employeeId, int salary) :
            employeeName(std::move(employeeName)), employeeID(employeeId), Salary(salary) {}
};

void DisplayEmployeesRecord(employee *a[], int s) {
    for (int i = 0; i < s; i++) {
        std::cout << a[i]->employeeName << " " << a[i]->employeeID << " " << a[i]->Salary << std::endl;
    }
}

void StoreToFile(employee a[], std::ofstream &f, int s) {
    for (int i = 0; i < s; i++) {
        f << a[i].employeeName << " " << a[i].employeeID << " " << a[i].Salary << std::endl;
    }
}


void StoreToArray(employee *a[], std::ifstream &f, int s) {
    for (int i = 0; i < s; i++) {
        std::string name;
        signed int id, salary;
        f >> name >> id >> salary;

        a[i] = new employee(name, id, salary);
    }
}


int main() {
    
    //Creating an array with random data
    employee array[10] = {
            employee("Emp1", 1, 1),
            employee("Emp2", 2, 100),
            employee("Emp3", 3, 100),
            employee("Emp4", 4, 1000),
            employee("Emp5", 5, 10000),
            employee("Emp6", 6, 100000),
            employee("Emp7", 7, 1000000),
            employee("Emp8", 8, 10000000),
            employee("Emp9", 9, 100000000),
            employee("Emp10", 10, 10000000),
    };

    //Write the array to a file
    std::ofstream OutputFile;
    OutputFile.open("EmployeeRecords.txt");
    StoreToFile(array, OutputFile, 10);
    OutputFile.close();

    //Create another array from the file
    std::ifstream InputFile;
    employee *ArrayToread[10];
    InputFile.open("EmployeeRecords.txt", std::ios_base::in);
    StoreToArray(ArrayToread, InputFile, 10);
    InputFile.close();

    //Display the created array
    DisplayEmployeesRecord(ArrayToread, 10);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog