/* payroll program with tax deduction * redirect cin to a file and use a separate output file */ #include #include using namespace std; int main() { int id; //employee id double hours,rate,pay,tax,netpay; int numemp = 0; //count the employees //un-comment to redirect cin to a file //ifstream cin("c:\\bc\\CISC1110\\pgms\\chapter3\\myinput.txt"); // 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 outfile.setf(ios::fixed,ios::floatfield); outfile.precision(2); //set decimal precision cout << "Please enter an ID (enter -1 to stop): "; cin >> id; //read ID while (id != -1) { //check for fake ID cout << "Please enter the hours worked: "; cin >> hours; //read hours worked cout << "Please enter the rate of pay: "; cin >> 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): "; cin >> id; //read new ID } outfile << "\nWe have processed " << numemp << " employees" << endl; //un-comment when redirecting cin to a file // cin.close(); //close the input file outfile.close(); //close the output file // system("pause"); //un-comment when using DevC++ return 0; }