C++ program with Vectors 1) Create a structure, Employee, with the following dat
ID: 3885553 • Letter: C
Question
C++ program with Vectors 1) Create a structure, Employee, with the following data struct Employee { string name: log SSN: float Income: float taxRate: }: 2) Create a vector of Employee structs, called Employees. 3) Need functions Read the data from a data file "employees.txt" in the vector Employees Determine the yearly tax and net income for a given Employee Print all personal and income information for every Employee in the vector Input information stored in "employees.txt" file: Adam 335234567 45000 0.19 Bob 234761344 60000 0.23Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<iomanip>
using namespace std;
typedef struct
{
string name;
long SSN;
float Income;
float taxRate;
}Employee;
int main()
{
vector<Employee> emp;
ifstream in;
//open file employee.txt
in.open("employees.txt");
//check file is opened otherwise print error msg
if (!in)
{
cout << "Input file cannot be opened" << endl;
return -1;
}
//now read from file into emp vector
float netincome,taxes;
Employee tmp;
cout << "Name" << " " << "SSN" << " " << "GrossIncome" << " " << "TaxRate" << " " << "Taxes" << " "<<"NetIncome" << endl;
while (!in.eof())
{
in >> tmp.name >> tmp.SSN >>tmp.Income >> tmp.taxRate;
emp.push_back(tmp);
taxes = tmp.Income*tmp.taxRate;
netincome =tmp.Income - taxes;
cout << tmp.name << " " << tmp.SSN << " " << tmp.Income << " " << tmp.taxRate << " " << taxes << " " << netincome << endl;
//cout << tmp.name << " " << tmp.SSN << " " << tmp.Income << " " << tmp.taxRate << " " << taxes << " " << netincome << endl;
}
}
--------------------------------------------------------------------------------------------------------------------
Name SSN GrossIncome TaxRate Taxes NetIncome
Adam 335234567 45000 0.19 8550 36450
Bob 234761344 60000 0.23 13800 46200
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.