// Pay calculator // Version 2.1 // // New in this version: Time is now a class with methods. #include using std::ifstream; using std::ofstream; #include using std::fixed; using std::setprecision; #include using std::cout; using std::cin; using std::istream; using std::ostream; #include using std::string; const double kPayRate = 12; class Time { public: void initialize() { hours = minutes = 99; } void read(istream & in) { in >> hours; in.get(); // colon in>> minutes; } void print(ostream & out) { out << hours << ':'; if (minutes < 10) out << 0; out << minutes; } double minus(const Time & t2) { return (hours + minutes/60.0) - (t2.hours + t2.minutes/60.0); } private: int hours; int minutes; }; int main() { cout << "Name of input file: "; string input_file_name; getline(cin, input_file_name); ifstream ifs_input(input_file_name); if (!ifs_input) { cout << "Could not open file.\n"; return 1; } cout << "Name of output file: "; string output_file_name; getline(cin, output_file_name); ofstream ofs_output(output_file_name); if (!ofs_output) { cout << "Could not open output file.\n"; return 1; } int employee_number; while (ifs_input >> employee_number) { Time start_time; start_time.read(ifs_input); Time stop_time; stop_time.read(ifs_input); double pay = stop_time.minus(start_time) * kPayRate; ofs_output << employee_number << ' '; start_time.print(ofs_output); ofs_output << ' '; stop_time.print(ofs_output); ofs_output << " $" << fixed << setprecision(2) << pay << '\n'; } return 0; }