/* payroll program with tax deductions * illustrate file I/O */ #include #include using namespace std; int main() { int id; //employee id double hours,rate,pay,tax,netpay; int numemp = 0; //count the employees // declare and open the input file ifstream infile("c:\\bc\\CISC1110\\pgms\\chapter3\\myinput.txt"); //comment-out for debugging //ifstream infile("con"); //un-comment for debugging //ifstream infile("/bc/CISC1110/pgms/chapter3/myinput.txt"); //for Mac users //ifstream infile("/dev/stdin"); //for Mac users // declare and open the output file ofstream outfile("c:\\bc\\CISC1110\\pgms\\chapter3\\myoutput.txt"); //comment-out for debugging //ofstream outfile("con"); //un-comment for debugging //ofstream outfile("/bc/CISC1110/pgms/chapter3/myoutput.txt"); //for Mac users //ofstream outfile("/dev/stdout"); //for Mac users outfile.setf(ios::fixed,ios::floatfield); outfile.precision(2); //set decimal precision outfile << "\t\t\tPayroll Table" << endl << endl; outfile << "Employee ID\tHours Worked\tRate of Pay\tGross Pay\tTax\tNet Pay" << endl; cout << "Please enter an ID (enter -1 to stop): "; infile >> id; //read ID while (id != -1) { //check for fake ID cout << "Please enter the hours worked: "; infile >> hours; //read hours worked cout << "Please enter the rate of pay: "; infile >> rate; //read rate of pay pay = hours * rate; //calculate pay tax = (pay < 300) ? 0.15 * pay : 0.28 * pay; //calculate tax netpay = pay - tax; //calculate the net pay cout << endl; //needed when output goes to monitor outfile.width(7); outfile << id; outfile.width(18); outfile << hours; outfile.width(16); outfile << rate; outfile.width(15); outfile << pay; outfile.width(12); outfile << tax; outfile.width(11); outfile << netpay << endl; numemp++; //increment counter cout << "Please enter an ID (enter -1 to stop): "; infile >> id; //read new ID } outfile << "\nWe have processed " << numemp << " employees" << endl; infile.close(); //close the input file outfile.close(); //close the output file // system("pause"); //un-comment when using DevC++ return 0; }