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

John Harris 1000 Lisa Smith 1002 Adam Johnson 1007 Sheila Smith 1009 Tristen Maj

ID: 3581786 • Letter: J

Question

John Harris 1000
Lisa Smith 1002
Adam Johnson 1007
Sheila Smith 1009
Tristen Major 1012
Yannic Lennart 1015
Lorena Emil 1018
Tereza Santeri 1020

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 First Name 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.g. 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

// C++ code
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string> // std::string, std::to_string
#include <math.h>
#include <fstream>

using namespace std;

struct Confidential
{
int id;
int SSN;
double salary;
};

struct Employee
{
string FirstName;
string LastName;
int id;
struct Confidential conf;
};


int main()
{
Employee s[20];
int size = 0;

ifstream inFile ("file1.txt");
if (inFile.is_open())
{
while(true)
{
if(inFile.eof())
break;

inFile >> s[size].FirstName >> s[size].LastName >> s[size].id;
size++;
}
inFile.close();
}
else cout << "Unable to open file";

int readID;
ifstream inFile1 ("file2.txt");
if (inFile1.is_open())
{
while(true)
{
if(inFile1.eof())
break;

inFile1 >> readID;
for (int i = 0; i < size; ++i)
{
if(s[i].id == readID)
{
s[i].conf.id = readID;
inFile1 >> s[i].conf.SSN;
inFile1 >> s[i].conf.salary;
}
}
}
inFile1.close();
}
else cout << "Unable to open file";

cout << "Employee details ";
for (int i = 0; i < size; ++i)
{
cout << s[i].id << " " << s[i].FirstName << " " << s[i].LastName << " " << s[i].conf.SSN << " " << s[i].conf.salary << endl;
}

return 0;
}


/*
file1.txt
John Harris 1000
Lisa Smith 1002
Adam Johnson 1007
Sheila Smith 1009
Tristen Major 1012
Yannic Lennart 1015
Lorena Emil 1018
Tereza Santeri 1020


file2.txt
1002 225365256 50000.00
1009 256214856 75000.00
1000 256314765 80000.00
1020 245123655 38000.00
1018 225453664 125000.00
1007 215236645 65800.00
1012 236456425 58000.00
1015 256452256 99000.00


output:
Employee details
1000 John Harris 256314765 80000
1002 Lisa Smith 225365256 50000
1007 Adam Johnson 215236645 65800
1009 Sheila Smith 256214856 75000
1012 Tristen Major 236456425 58000
1015 Yannic Lennart 256452256 99000
1018 Lorena Emil 225453664 125000
1020 Tereza Santeri 245123655 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