Write a complete C++ program to do the following: Read payroll information from
ID: 3864815 • Letter: W
Question
Write a complete C++ program to do the following: Read payroll information from a file (payroll.txt) until you reach the end of the file. For each employee read the person's first name, last name, and their annual earnings. Compute their tax owed and their net pay using their tax rate. Less than or equal to 20,000 Rate = 10% Greater than 20,000 but less than or equal to 50,000 Rate = 20% Greater than 50,000 but less than 100,000 Rate = 25% Greater than or equal to 100,000 Rate = 30% Print their name, their tax owed, and their net earnings. At the end print the total earnings of all employees and the total tax paid.Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream inputFile;
inputFile.open("payroll.txt");
string firstName, lastName;
int taxRate;
double totalEarning =0, totalTax = 0,earnings;
cout<<"First Name Last Name Net Pay Tax Owed"<<endl;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> firstName >>lastName>>earnings ;
if(earnings >= 100000){
taxRate = 30;
}
else if(earnings >= 50000 && earnings < 100000){
taxRate = 25;
}
else if(earnings >= 20000 && earnings < 50000){
taxRate = 20;
}
else{
taxRate = 10;
}
double taxOwed = (earnings*taxRate)/100;
double netPay =earnings - taxOwed;
totalTax = totalTax +taxOwed;
totalEarning = totalEarning +earnings;
cout<<firstName<<" "<<lastName<<" "<<netPay<<" "<<taxOwed<<endl;
}
cout<<"Total Earnings: "<<totalEarning<<endl;
cout<<"Total Tax: "<<totalTax<<endl;
}
inputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
First Name Last Name Net Pay Tax Owed
suresh murapaka 9000 1000
sekhar murapaka 32000 8000
anshu murapaka 9000 1000
revathi kella 45000 15000
Total Earnings: 120000
Total Tax: 25000
payroll.txt
suresh murapaka 10000
sekhar murapaka 40000
anshu murapaka 10000
revathi kella 60000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.