1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
class Presidents {
public:
string firstname;
string lastname;
string salary;
string id;
Presidents (const string &firstname,
const string &lastname,
const string &salary,
const string &id) {
this->firstname = firstname;
this->lastname = lastname;
this->salary = salary;
this->id = id;
}
};
int get_file (ifstream& in, ofstream& out, string filename, string output); //this function opens the input file and output file and checks to see if they are open
void get_line (ifstream& in, ofstream& out); //this function brings the input file into a string and then changes all lowercase letters to uppercasse and leaves uppercase letters alone and changes digits to a * symbol then outputs its a$
//void name (ifstream& in, ofstream& out);
//void salary (ifstream& in, ofstream& out);
int main () {
ifstream in; //instream
ofstream out; //outstream
string filename, output; //input and output file
get_file (in, out, filename, output);
get_line (in, out);
// name (in, out);
// salary (in, out);
}
int get_file (ifstream& in, ofstream& out, string filename, string output) {
cout << "enter the file you would like to open" << endl;
cin >> filename; //takes in the name of the file your opening
in.open (filename.c_str()); //opens the file
if (in.fail()) { //checks to see if the file is open
cout << "that is not a file" << endl;
return 0;
}
cout << "Input the name of the output file" << endl;
cin >> output;
out.open (output.c_str());
if (in.fail()) {
cout << "that is not a file" << endl;
return 0;
}
return 0;
}
void get_line (ifstream& in, ofstream& out) {
std::vector <Presidents> sort;
string line;
double total;
out << "Sorted by name" << endl;
out << "ID Name Salary" << endl;
out << "------------------------------------" << endl;
while (getline (in, line)){ // using getline to read in a single line at a time
string id, firstname, lastname, salary;
istringstream iss (line);
iss >> id >> firstname >> lastname >> salary;
Presidents person (firstname, lastname, salary, id);
sort.push_back (person);
}
std::sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });
for (Presidents i : sort) {
out << i.id << " " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
}
out << endl << endl;
out << "Sorted by salary" << endl;
out << "ID Name Salary" << endl;
out << "------------------------------------" << endl;
while (getline (in, line)){ // using getline to read in a single line at a time
string id, firstname, lastname, salary;
int money = 0;
istringstream iss (line);
iss >> id >> firstname >> lastname >> salary >> total;
if ( ! (istringstream(salary) >> money)) money = 0;
Presidents person (firstname, lastname, salary, id);
sort.push_back (person);
total = money + total;
}
std::sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.salary < b.salary; });
for (Presidents i : sort) {
out << i.id << " " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
}
out << endl << "Total Payroll $" << total << endl;
in.close (); //closes the input file
out.close (); //closes the output file
}
|