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

Define a structure called Employee with four member variables- first name, last

ID: 3835174 • Letter: D

Question

Define a structure called Employee with four member variables- first name, last name, ID and salary. Use appropriate data types. You are provided a data file named Hwi0Probl dat with four parameters in the format of: FirstName LastName 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<stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

//define the structure to be used
struct Employee
{
string fname;
string lname;
int id;
float salary;
};

int main()
{
Employee emp; //instantiate employee structure
string line;
float avgSalary=0;
   float totalSalary=0;
ifstream myfile ("C:\HW10Prob1.dat"); //instantiate input file
int records=0;
  
if (myfile.is_open())
{
    //if file is open then read from file
while ( getline (myfile,line) )
{
string parsed;
stringstream input_stringstream(line);
int counter =0;
  
//parse the line and assign it to emp
while(getline(input_stringstream,parsed,' '))
{
if(counter==0){
    emp.fname=parsed;
}else if(counter==1){
    emp.lname=parsed;
}else if(counter == 2){
    emp.salary=atof( parsed.c_str() );
}else if(counter == 3){
    emp.id=atoi( parsed.c_str() );
}
counter++;
}
cout << emp.fname << ' ' << emp.lname << ' ' << emp.salary << ' ' << emp.id << ' ';
totalSalary=totalSalary+emp.salary;
records++;
}
  
myfile.close();
}
  
cout << "Average Salary" << totalSalary/records << ' ';
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote