In: Computer Science
Working with Files in C++.
Create the following program called payroll.cpp. Note that the file you read must be created before you run this program. The output file will be created automatically by the program. You can save the input file in the same directory as your payroll.cpp file by using Add New Item, Text File.
// File: Payroll.cpp
// Purpose: Read data from a file and write out a payroll
// Programmer: (your name and section)
#include <cstdlib> // for the definition of EXIT_FAILURE
#include <fstream> // required for external file streams
#include <iostream> // required for cin cout
using namespace std;
int main ()
{
ifstream ins; // associates ins as an input stream
ofstream outs; // associates outs as an output stream
int id; // id for employee
double hours, rate; // hours and rate worked
double pay; // pay calculated
double total_pay; // grand total of pay
// Open input and output file, exit on any error
ins.open ("em_in.txt"); // ins connects to file "em_in.txt"
if (ins.fail ())
{
cout << "*** ERROR: Cannot open input file. " << endl;
getchar(); // hold the screen
return EXIT_FAILURE;
} // end if
outs.open ("em_out.txt"); // outs connects to file "em_out.txt"
if (outs.fail ())
{
cout << "*** ERROR: Cannot open output file." << endl;
getchar();
return EXIT_FAILURE;
} // end if
// Set total_pay to 0
total_pay = 0;
ins >> id; // get first id from file
// Do the payroll while the id number is not the sentinel value
while (id != 0)
{
ins >> hours >> rate;
pay = hours * rate;
total_pay += pay;
outs << "For employee " << id << endl;
outs << "The pay is " << pay << " for " << hours
<< " hours worked at " << rate << " rate of pay" << endl << endl;
ins >> id;
} // end while
// Display a message on the screen
cout << "Employee processing finished" << endl;
cout << "Grand total paid out is " << total_pay << endl;
ins.close(); // close input file stream
outs.close(); // close output file stream
return 0;
}
Create the input file:
Inside C++ go to File Add New Item and then Text to create a text file.
Type in the data below
In the same directory as your .cpp file for Payroll.cpp click Files and Save As em_in.txt
1234
35 10.5
3456
40 20.5
0
solution should show :
-the output file
-the input file
-the screen output
-the source program
Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
source file, properly formatted:
// File: Payroll.cpp // Purpose: Read data from a file and write out a payroll // Programmer: (your name and section) #include <cstdlib> // for the definition of EXIT_FAILURE #include <fstream> // required for external file streams #include <iostream> // required for cin cout using namespace std; int main() { ifstream ins; // associates ins as an input stream ofstream outs; // associates outs as an output stream int id; // id for employee double hours, rate; // hours and rate worked double pay; // pay calculated double total_pay; // grand total of pay // Open input and output file, exit on any error ins.open("em_in.txt"); // ins connects to file "em_in.txt" if (ins.fail()) { cout << "*** ERROR: Cannot open input file. " << endl; getchar(); // hold the screen return EXIT_FAILURE; } // end if outs.open("em_out.txt"); // outs connects to file "em_out.txt" if (outs.fail()) { cout << "*** ERROR: Cannot open output file." << endl; getchar(); return EXIT_FAILURE; } // end if // Set total_pay to 0 total_pay = 0; ins >> id; // get first id from file // Do the payroll while the id number is not the sentinel value while (id != 0) { ins >> hours >> rate; pay = hours * rate; total_pay += pay; outs << "For employee " << id << endl; outs << "The pay is " << pay << " for " << hours << " hours worked at " << rate << " rate of pay" << endl << endl; ins >> id; } // end while // Display a message on the screen cout << "Employee processing finished" << endl; cout << "Grand total paid out is " << total_pay << endl; ins.close(); // close input file stream outs.close(); // close output file stream return 0; }
em_in.txt file (should be placed in the same directory as your cpp file)
1234 35 10.5 3456 40 20.5 0
em_out.txt file (generated after running the program)
For employee 1234 The pay is 367.5 for 35 hours worked at 10.5 rate of pay For employee 3456 The pay is 820 for 40 hours worked at 20.5 rate of pay
console output generated after running the program
Employee processing finished Grand total paid out is 1187.5
screenshots of everything