Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help solve with c++ Define a structure called Employee with four member variable

ID: 3838218 • Letter: H

Question


help solve with c++

Define a structure called Employee with four member variables- first name, last name. ID and salary. Use appropriate datatypes. You are provided a data file named HW 10Prob1.dat with four parameters in the format of: First Name Last Name Salary ID Declare an instance of the structure and name it emp. Read the appropriate parameter from the file, assign the read parameter to the correct variable and display them on the screen. Also, calculate the average salary and display on screen. Don't worry about arrays in this problem, you can display as you read them, one line at a time.

Explanation / Answer

#include<iostream>
#include <fstream>

struct Employee {
   string firstName;
   string lastName;
   double salary;
   int ID;
}

void read() {
   ifstream myfile;
   Employee emp;
   double tot = 0;
   int count = 0;
   myfile.open ("10Prob1.dat");
   while (!myfile.eof) {
       myfile >> emp.firstName;
       myfile >> emp.lastName;
       myfile >> emp.salary;
       myfile >> emp.id;
       tot += emp.salary;
       count ++;
       cout << emp.firstName << " " << emp.lastName << " " << emp.salary << " " << emp.id << endl;
   }
   double avg = tot/count;
   cout << "Average: " << avg << endl;
}

This code will help you. Let me know if you face any problem with the code. I shall try my best to resolve all your queries.