Answer to Question #181545 in C++ for nabila

Question #181545

Write a program to calculate the net pay of an employee. Input the basic pay, pass basic pay to user defined function PayCalculate(), calculate net pay and return to main function. Calculate the net pay of an employee should be stored in a file along with its complete detail such as emp_no, emp_name, emp-designation and next time if user want to view it can easily view it.

House Rent is 45% of basic pay.

Medical Allowance is 5% of basic pay if pay is greater than Rs.4000, it is 7% of basic pay if basic pay is less than or equal to Rs.4000.

Conveyance Allowance is Rs.200 of basic pay if pay is greater than Rs.4000, it is Rs.435 of basic pay if basic pay is less than or equal to Rs.4000.

Net pay is calculated by adding house rent, medical allowance ,conveyance allowance and basic pay.


1
Expert's answer
2021-04-14T23:01:32-0400
#include <iostream>
#include <fstream>
#include <string>


using namespace std;
double PayCalculate(double basicPay);


int main()
{
	double basicPay;
	double netPay;
	int emp_no;
	string emp_name;
	string designation;
	const string fileName="employee.txt"; 
	//Calculate the net pay of an employee should be stored in a file along with its complete detail such as 
	//emp_no, emp_name, emp-designation and next time if user want to view it can easily view it.
	int ch=0;
	while(ch!=3){
		//Display main menu
		cout<<"1. Calculate the net pay of a new employee\n";
		cout<<"2. View the employee info in the file\n";
		cout<<"3. Exit\n";
		cout<<"Your choice: ";
		cin>>ch;
		if(ch==1){
			cout<<"Enter the employee no: ";
			cin>>emp_no;
			cin.ignore();
			cout<<"Enter the employee name: ";
			getline(cin,emp_name);
			cout<<"Enter the employee designation: ";
			getline(cin,designation);
			cout<<"Enter the employee basic pay: ";
			cin>>basicPay;
			//Calculate the net pay of a new employee
			netPay=PayCalculate(basicPay);
			cout<<"The employee net pay: Rs."<<netPay<<"\n\n";
			//save to the file "employee.txt"
			ofstream employeefile;
			employeefile.open(fileName);
			employeefile <<emp_no<< "\n";
			employeefile <<emp_name<< "\n";
			employeefile <<designation<< "\n";
			employeefile <<basicPay<< "\n";
			employeefile <<netPay<< "\n";
			employeefile.close();
		}else if(ch==2){
			ifstream ifstreamEmployee(fileName);
			if (!ifstreamEmployee){
				cout << "The file '"<<fileName<<"' does not exist.\n";
			}else{
				string line;
				//read the employee no from the file
				getline(ifstreamEmployee, line);
				cout<<"Enter the employee no: "<<line<<"\n";
				//read the employee name from the file
				getline(ifstreamEmployee, line);
				cout<<"Enter the employee name: "<<line<<"\n";
				//read the employee designation from the file
				getline(ifstreamEmployee, line);
				cout<<"Enter the employee designation: "<<line<<"\n";
				//read the employee basic pay from the file
				getline(ifstreamEmployee, line);
				cout<<"Enter the employee basic pay: Rs."<<line<<"\n";
				//read the employee net pay from the file
				getline(ifstreamEmployee, line);
				cout<<"The employee net pay: Rs."<<line<<"\n\n";
				//close the file
				ifstreamEmployee.close();
			}


		}else{
			cout<<"Select a correct menu item.\n\n";
		}
	}
	return 0;
}
//pass basic pay to user defined function PayCalculate(), calculate net pay and return to main function.
double PayCalculate(double basicPay){
	//House Rent is 45% of basic pay.
	double houseRent=0.45*basicPay;
	//Medical Allowance is 5% of basic pay if pay is greater than Rs.4000, it is 7% of basic pay if basic pay is less than or equal to Rs.4000.
	double medicalAllowance=0.05*basicPay;
	//Conveyance Allowance is Rs.200 of basic pay if pay is greater than Rs.4000, it is Rs.435 of basic pay if basic pay is less than or equal to Rs.4000.
	double conveyanceAllowance=200;
	if(basicPay<=4000){
		medicalAllowance=0.07*basicPay;
		conveyanceAllowance=435;
	}
	//Net pay is calculated by adding house rent, medical allowance ,conveyance allowance and basic pay.
	return houseRent+medicalAllowance+conveyanceAllowance+basicPay;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS