5

C++ salary calculation formula trouble

 2 years ago
source link: https://www.codeproject.com/Questions/5320553/Cplusplus-salary-calculation-formula-trouble
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

See more:

hello, I am looking for some assistance in fixing a calculation string in my C++ code.

the issue that I am having is my monthly gross salary is not outputting correctly. I have created my inData.txt file with the following information:

Giselle Robinson Accounting
5600 5 30
450 9
75 1.5

my output should read:

Copy Code
Monthly Gross Salary : $ 5600.00, Monthly Bonus : 5.00%, Taxes : 30.00%
Paycheck : $4116.00


the incorrect output that i am getting is:
Copy Code
Monthly Gross Salary : $ 5880.00, Monthly Bonus : 5.00%, Taxes : 30.00%
Paycheck : $4116.00


I am almost positive that my issue is with my calculation for monthly salary and paycheck. can somebody assist me with fixing this please?

What I have tried:

everything else works and outputs exactly how it should be.

here is my code:
Expand ▼   Copy Code
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdio>

using namespace std;

int main()
{
	ifstream inFile("C:/Users/Student/Desktop/CS265 programming in C++/weekly assignments/week 2 assignment/week 2 assignment/week 2 assignment/inData.txt");
	ofstream outFile("C:/Users/Student/Desktop/CS265 programming in C++/weekly assignments/week 2 assignment/week 2 assignment/week 2 assignment/outData.txt");

	outFile.open("C:/Users/Student/Desktop/CS265 programming in C++/weekly assignments/week 2 assignment/week 2 assignment/week 2 assignment/outData.txt");

	if (inFile.is_open())
	{
		cout << "File opened\n" << endl;
	}
	else
	{
		perror("File Failed to open");
		return 1;
	}

	//Setting the decimal value to .00

	{
		std::cout << std::fixed;
		std::cout << std::setprecision(2);
	}

	string firstName,
		lastName,
		department;

	double monthlySalary,
		monthlyBonus,
		taxes,
		payCheck,
		distanceTraveled,
		travelingTime,
		coffeeCost,
		coffeeSales,
		averageSpeed;

	int cupsSold;

	//File Input

	inFile >> firstName >> lastName >> department;
	inFile >> monthlySalary >> monthlyBonus >> taxes;
	inFile >> distanceTraveled >> travelingTime;
	inFile >> cupsSold >> coffeeCost;

	//Calculate the monthly salary

	monthlySalary = monthlySalary + (monthlyBonus / 100 * monthlySalary);
	payCheck = monthlySalary - (taxes / 100 * monthlySalary);
	averageSpeed = distanceTraveled / travelingTime;
	coffeeSales = cupsSold * coffeeCost;

	//Output to file

	outFile << "Name : " << firstName + ' ' + lastName << ", Department : " << department << endl;
	outFile << "Monthly Gross Salary : $ " << monthlySalary << ", Monthly Bonus : " << monthlyBonus << "%, Taxes : " <<taxes << "%" << endl;
	outFile << "Paycheck : $" << payCheck << endl;
	outFile << "distance traveled : " << distanceTraveled << " miles" << ", traveling time: " << travelingTime << ", hours" << endl; 
	outFile << "Average Speed : " << averageSpeed << " mph" << endl;
	outFile << "number of coffee cups sold: " << cupsSold << ", cost: $ " << coffeeCost << " per cup " << endl;
	outFile << "Sales Amount : $" << coffeeSales << endl;

	//close files

	inFile.close();
	outFile.close();

	cout << "Name : " << firstName + ' ' + lastName << ", Department : " << department << endl;
	cout << "Monthly Gross Salary : $ " << monthlySalary << ", Monthly Bonus : " << monthlyBonus << "%, Taxes : " <<taxes << "%" << endl;
	cout << "Paycheck : $" << payCheck << endl;
	cout << "distance traveled : " << distanceTraveled << " miles" << ", traveling time: " << travelingTime << ", hours" << endl;
	cout << "Average Speed : " << averageSpeed << " mph" << endl;
	cout << "number of coffee cups sold: " << cupsSold << ", cost: $ " << coffeeCost << " per cup " << endl;
	cout << "Sales Amount : $" << coffeeSales << endl;

	cin.get();

	return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK