// Pay calculator // Version 2 // // New in this version: Data abstraction is enforced by making // Time's data private. #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 { friend void initialize(Time & t); friend void read(Time & t, istream & in); friend void print(const Time & t, ostream & out); friend double difference(const Time & t1, const Time & t2); private: int hours; int minutes; }; void initialize(Time & t) { t.hours = t.minutes = 99; } void read(Time & t, istream & in) { in >> t.hours; in.get(); // colon in>> t.minutes; } void print(const Time & t, ostream & out) { out << t.hours << ':'; if (t.minutes < 10) out << 0; out << t.minutes; } double difference(const Time & t1, const Time & t2) { return (t1.hours + t1.minutes/60.0) - (t2.hours + t2.minutes/60.0); } 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; read(start_time, ifs_input); Time stop_time; read(stop_time, ifs_input); double pay = difference(stop_time, start_time) * kPayRate; ofs_output << employee_number << ' '; print(start_time, ofs_output); ofs_output << ' '; print(stop_time, ofs_output); ofs_output << " $" << fixed << setprecision(2) << pay << '\n'; } return 0; }