// run_pay_calculator.cpp #include "run_pay_calculator.h" #include using std::ifstream; using std::ofstream; #include using std::cin; using std::cout; using std::endl; #include using std::fixed; using std::setprecision; using std::ws; #include using std::string; //using std::to_string; #include using std::istringstream; using std::ostringstream; #include "Time.h" inline string to_string(int x) { ostringstream oss; oss << x; return oss.str(); } void run_pay_calculator() { cout << "Name of input file: "; string input_file_name; getline(cin, input_file_name); ifstream ifs_input(input_file_name); if (!ifs_input) throw PayCalculatorError("File " + input_file_name + " did not open"); cout << "Name of output file: "; string output_file_name; getline(cin, output_file_name); ofstream ofs_output(output_file_name); if (!ofs_output) throw PayCalculatorError("File " + output_file_name + " did not open"); string s_line; int line_number = 0; while (getline(ifs_input, s_line)) { ++line_number; // extract data from line int employee_number = -1; Time start_time, stop_time; istringstream iss_line(s_line); iss_line >> employee_number >> start_time >> stop_time; // check if an error occurred if (!iss_line) throw PayCalculatorError("Error in input file at line " + to_string(line_number)); // check that rest of line is blank iss_line >> ws; if (!iss_line.eof()) throw PayCalculatorError("Error in input file at line " + to_string(line_number)); // compute pay double pay = (stop_time - start_time) * kPayRate; // print results ofs_output << employee_number << ' ' << start_time << ' ' << stop_time << " $" << fixed << setprecision(2) << pay << '\n'; } }