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
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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.