// Pay calculator // Version 2.2 // // New in this version: uses Version 1.4 of class Time, which // includes, in particular, constructors and operators. #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: friend istream & operator>>(istream & in, Time & t); Time() : Time(99, 99) {} Time(int h) : Time(h, 0) {} Time(int h, int m) : hours_(h), minutes_(m) {} int hours() const { return hours_; } int minutes() const { return minutes_; } void set_hours(int new_hours) { hours_ = new_hours; } void set_minutes(int new_minutes) { minutes_ = new_minutes; } void set(int new_hours, int new_minutes = 0); private: int hours_; int minutes_; }; inline void Time::set(int new_hours, int new_minutes) { hours_ = new_hours; minutes_ = new_minutes; } inline double operator-(const Time & t1, const Time & t2) { return (t1.hours() + t1.minutes()/60.0) - (t2.hours() + t2.minutes()/60.0); } inline istream & operator>>(istream & in, Time & t) { in >> t.hours_; in.get(); // colon in >> t.minutes_; return in; } inline ostream & operator<<(ostream & out, const Time & t) { out << t.hours() << ':'; if (t.minutes() < 10) out << '0'; out << t.minutes(); return out; } 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 = -1; Time start_time, stop_time; while (ifs_input >> employee_number) { ifs_input >> start_time >> stop_time; double pay = (stop_time - start_time) * kPayRate; ofs_output << employee_number << ' ' << start_time << ' ' << stop_time << " $" << fixed << setprecision(2) << pay << '\n'; } return 0; }