/* 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 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 outfile << "Employee " << id << " worked " << hours << " hours at a rate of pay of $" << rate << " earning $" << pay << endl; outfile << "tax withheld was $" << tax << " leaving net pay" <<" of $" << netpay << endl << 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; }