Using C++ Define a structure called Employee with four member variables- first n
ID: 3835653 • Letter: U
Question
Using C++
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 HW10Prob1.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
HW10Prob1.dat
John Harris 50000 113785
Lisa Smith 75000.487 254785
Adam Johnson 68500 321258
Sheila Smith 150000.50 165478
Tristen Major 75800.76 102596
Yannic Lennart 58000.55 369854
Lorena Emil 43000.00 225478
Tereza Santeri 48000.001 136478
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
struct Employee {
string lastName;
string firstName;
double salary;
int id;
};
struct Employee emp;
int main() {
int i = 0;
ifstream inputFile;
double sum = 0;
inputFile.open("HW10Prob1.dat");
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile>>emp.lastName>>emp.firstName>>emp.salary>>emp.id;
cout<<"Last Name: "<<emp.lastName<<" "<<"First Name: "<<emp.firstName<<" "<<"Salary: "<<emp.salary<<" "<<"ID: "<<emp.id<<endl;
sum = sum + emp.salary;
i++;
}
}
cout<<"Average Salary: "<<sum/i<<endl;
inputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Last Name: John First Name: Harris Salary: 50000 ID: 113785
Last Name: Lisa First Name: Smith Salary: 75000.5 ID: 254785
Last Name: Adam First Name: Johnson Salary: 68500 ID: 321258
Last Name: Sheila First Name: Smith Salary: 150000 ID: 165478
Last Name: Tristen First Name: Major Salary: 75800.8 ID: 102596
Last Name: Yannic First Name: Lennart Salary: 58000.6 ID: 369854
Last Name: Lorena First Name: Emil Salary: 43000 ID: 225478
Last Name: Tereza First Name: Santeri Salary: 48000 ID: 136478
Average Salary: 71037.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.