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

C++ problem file1.txt file2.txt Problem 1. Structures (10 points) A company keep

ID: 3575402 • Letter: C

Question

C++ problem

file1.txt

file2.txt

Problem 1. Structures (10 points) A company keeps its employee databases in two text files. The first file contains the first name, the last name and ID in the following format FirstName LastName ID This is provided to you as filel.txt The second file contains the ID, the SSN and the salary of the employee ID SSN Salary This is provided to you in file2.txt. Note that the order of the IDs may not be the same in both files Design a program with a nested structure. The outer structure is named Employee and has variables for first name, last name and salary. The inner structure is called Confidential and has variables for SSN and salary Declare an array of structures in your main function. For e Employee E[20] Assume there are no more than 20 employees in the company. After reading both files and paring the data by ID, display the first name, the last name, the SSN and the salary of each employee.

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
//Outer Structure for Employee
struct Employee
{
//Data member for first name, last name, and salary
string firstName, lastName;
float salary;
//Inner Structure for Confidential
struct Confidential
{
//Data member for social security number and salary
int ssn;
float salary;
}C[20]; //For 20 confidential record
}E[20]; //For 20 Employee record

int main()
{
//ifstream object to read file
ifstream myfile1, myfile2;
//Open the file1.txt file
myfile1.open("file1.txt");
int c = 0, f1id, f2id, fssn;
float fsalary;
string ffn, fln;
//Checks whether file can be opened or not
if (myfile1.is_open())
{
//Loops till end of file1.txt
while (!myfile1.eof())
{
//Reads data from file1.txt
myfile1>>ffn;
myfile1>>fln;
myfile1>>f1id;
//Open the file2.txt file
myfile2.open("file2.txt");
//Checks whether file can be opened or not
if(myfile2.is_open())
{
//Loops till end of file2.txt
while(!myfile2.eof())
{
//Reads data from file2.txt
myfile2>>f2id;
myfile2>>fssn;
myfile2>>fsalary;
//Compares the id of file1.txt and file2.txt if match then store data in employee and confidential structure object
if(f1id == f2id)
{
//E for Employee structure
E[c].firstName = ffn;
E[c].lastName = fln;
E[c].salary = fsalary;
//C for Confidential structure which is nested inside the Employee. So E[c].C[c]
//Where c is the index position
E[c].C[c].ssn = fssn;
E[c].C[c].salary = fsalary;
c++;//Counter for number of records
}
}
//Close file2.txt
myfile2.close();
}
//Error if file unable to open
else
cout<<"Error: In open file2.txt ";
}
//Close file1.txt
myfile1.close();
}
//Error if file unable to open
else
cout<<"Error: In open file1.txt";
//Display the Match id information as per the requirement
for(int x = 0; x < c; x++)
{
cout<<" First name: "<<E[x].firstName<<" Last Name: "<<E[x].lastName<<" SSN: "<<E[x].C[x].ssn<<" Salary: "<<E[x].C[x].salary<<endl;
}
}

Output:

First name: John Last Name: Harris SSN: 256314765 Salary: 80000

First name: Lisa Last Name: Smith SSN: 225365256 Salary: 50000

First name: Adam Last Name: Johnson SSN: 215236645 Salary: 65800

First name: Sheila Last Name: Smith SSN: 256214856 Salary: 75000

First name: Tristen Last Name: Major SSN: 236456425 Salary: 58000

First name: Yannic Last Name: Lennart SSN: 256452256 Salary: 99000

First name: Lorena Last Name: Emil SSN: 225453664 Salary: 125000

First name: Tereza Last Name: Santeri SSN: 245123655 Salary: 38000

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