Consider the following C++ struct which gathers a company\'s information about a
ID: 673370 • Letter: C
Question
Consider the following C++ struct which gathers a company's information about an employee into one data item:
struct EmployeeInfo{
string lastName;
string firstName;
double salary;
double percentIncrease;
double newSalary;
};
Write a program for a company of no more than ten employees, keeping their data in an array of these structs (one for each employee).
The program is to read a data file (sample attached), containing one line for each employee with
Employee last name
Employee first name
Employee current salary
Employee percentage raise
Once an employee's current salary and percentage raise are known, the program can compute and fill in the employee's "newSalary".
Once this computation is done, report each employee's name (first name *first*, then last) and new salary. Report with exactly two digits to the right of the decimal point, even if not "necessary" (i.e. if my new salary is exactly one hundred dollars, report it as $ 100.00, not $100.)
File: lab9Data.txt
Explanation / Answer
For this we shall use, array of structs. This is how our main function would work
EmployeeInfo *emp=new EmployeeInfo[10];
fstream infile("yourfile.txt");
string line,fullName;
char *ptr;
for(int i=0;i<10;i++)
{
get(infile,line);
while (token = strtok(line, “ ”))
{
emp[i]->lastName=strtok(NULL, “ ”);
emp[i]->firstName= strtok(NULL, “ ”);
emp[i]->salary= (double) strtok(NULL, “ ”);
emp[i]->percentIncrease= (double) strtok(NULL, “ ”);
//calculate new salary
newSalary= (emp[i]->percentIncrease* emp[i]->salary)/100;
emp[i]->newSalary= newSalary;
token;
continue;
}
}
}
for(int i=0;i<10;i++)
{
fullname=emp[i]->firstName+” “+ emp[i]->lastName;
cout<<”Full Name of Emoloyee”<<fullName;
cout.precision(2);
cout<<”Salary of Emoloyee”<<emp[i]->newSalary;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.