1. Please help! :) Simple C++ Programming. Please make sure it compiles. FinalPr
ID: 3581824 • Letter: 1
Question
1. Please help! :) Simple C++ Programming. Please make sure it compiles.
FinalProb1.txt:
John Harris 1000 50000.00
Lisa Smith 1002 75000.00
Adam Johnson 1007 96000.00
Sheila Smith 1009 39580.00
Tristen Major 1012 115000.00
Yannic Lennart 1015 96000.00
Lorena Emil 1018 69000.00
Tereza Santeri 1020 85000.00
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;
void sort(string lastName[], string firstName[], int id[], double salary[], int size)
{
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if(salary[i] < salary[j])
{
int t = id[i];
id[i] = id[j];
id[j] = t;
string s = firstName[i];
firstName[i] = firstName[j];
firstName[j] = s;
s = lastName[i];
lastName[i] = lastName[j];
lastName[j] = s;
double r = salary[i];
salary[i] = salary[j];
salary[j] = r;
}
}
}
}
int main()
{
string lastName[20];
string firstName[20];
int id[20];
double salary[20];
int size = 0;
ifstream inFile ("FinalProb1.txt");
if (inFile.is_open())
{
while(true)
{
if(inFile.eof())
break;
inFile >> firstName[size] >> lastName[size] >> id[size] >> salary[size];
size++;
}
inFile.close();
}
else cout << "Unable to open file";
sort(lastName, firstName, id, salary, size);
cout << "Sorted Employee details ";
for (int i = 0; i < size; ++i)
{
cout << id[i] << " " << firstName[i] << " " << lastName[i] << " " << salary[i] << endl;
}
return 0;
}
/*
FinalProb1.txt:
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
output:
Sorted Employee details
1009 Sheila Smith 39580
1000 John Harris 50000
1018 Lorena Emil 69000
1002 Lisa Smith 75000
1020 Tereza Santeri 85000
1007 Adam Johnson 96000
1015 Yannic Lennart 96000
1012 Tristen Major 115000
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.