This code was inspired by this question: Payroll Program.  I saw lots of ways I thought the code could be improved.  So much so that I thought it wouldn't be particularly constructive to post all of it as an answer to that question.  Essentially, I wanted to basically rewrite the whole thing from scratch.So I did.  But I'm far from a C++ expert, so tell me how to make this even better:#include <cstdlib>#include <iostream>#include <fstream>#include <string>#include <iomanip>#include <vector>#include <sstream>const double WORK_WEEK_HOURS = 40;const double OVERTIME_RATE = 1.5;struct employee {    int id;    double payrate;    double hours;};void payroll();std::string fetchFileName();employee fetchEmployee();double wagesForEmployee(employee e);bool askUser(std::string s);std::string employeesToString(std::vector<employee> e);void displayEmployees(std::vector<employee> e);void saveEmployeesToFile(std::vector<employee> e);int main(int argc, const char * argv[]) {    payroll();    return 0;}void payroll() {    std::vector<employee> employees;    do {        employees.push_back(fetchEmployee());    } while (askUser(Would you like to enter more employees? (y/n): ));    if (askUser(Would you like to view the information you entered? (y/n): )) {        displayEmployees(employees);    }    if (askUser(Would you like to save the information you entered to file? (y/n): )) {        saveEmployeesToFile(employees);    }}bool askUser(std::string s) {    char answer;    std::cout << s;    std::cin >> answer;    return (answer == 'y' || answer == 'Y');}employee fetchEmployee() {    employee e;    e.id = -1;    e.payrate = -1;    e.hours = -1;    do {        std::cout << Enter the employee ID: ;        std::cin >> e.id;    } while(e.id < 0);    do {        std::cout << Enter the payrate: ;        std::cin >> e.payrate;    } while (e.payrate < 0);    do {        std::cout << Enter the hours worked: ;        std::cin >> e.hours;    } while (e.hours < 0);    return e;}double wagesForEmployee(employee e) {    if (e.hours < 0 || e.payrate < 0) {        return 0;    }    double wages = 0;    if (e.hours > WORK_WEEK_HOURS) {        wages += (e.hours - WORK_WEEK_HOURS) * OVERTIME_RATE * e.payrate;    }    wages += std::min(e.hours, WORK_WEEK_HOURS) * e.payrate;    return wages > 0 ? wages : 0;}std::string employeesToString(std::vector<employee> e) {    std::stringstream ss;    ss << std::setw(10) << ID << std::setw(10) << Rate << std::setw(10) << Hours << std::setw(10) << Wage << std::endl;    ss << --------------------------------------------------------- << std::endl;    for (int i = 0; i < e.size(); ++i) {        employee curEmp = e.at(i);        ss  << std::setw(10) << curEmp.id << std::setprecision(2) << std::fixed            << std::setw(10) << curEmp.payrate << std::setprecision(2) << std::fixed            << std::setw(10) << curEmp.hours << std::setprecision(2) << std::fixed            << std::setw(10) <<  wagesForEmployee(curEmp) << std::endl;    }    return ss.str();}void displayEmployees(std::vector<employee> e) {    std::cout << employeesToString(e) << std::endl;}void saveEmployeesToFile(std::vector<employee> e) {    std::ofstream outputFile;    std::string fileName;    std::cout << Enter a file name: ;    // TODO: Safety checking for illegal file name characters, etc.    std::cin >> fileName;    fileName += .pay;    outputFile.open(fileName.c_str());    outputFile << employeesToString(e);    outputFile.close();    std::cout << Successfully saved to  << fileName;}
Printing/Saving a Payroll Chart
c++;rags to riches