Click here to Skip to main content
15,302,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
Monthly Gross Salary : $ 5600.00, Monthly Bonus : 5.00%, Taxes : 30.00%
Paycheck : $4116.00


the incorrect output that i am getting is:
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:

#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;
}
Posted
Updated 21-Dec-21 3:54am
Comments
CHill60 21-Dec-21 9:16am
   
Your expected output and actual output look identical to me
nmeyer1 21-Dec-21 9:23am
   
my current output is displaying $5880.00 when it should be displaying $5600.00

C++
monthlySalary = monthlySalary + (monthlyBonus / 100 * monthlySalary);

You have added the bonus to the monthly salary before you print it. You should create a different variable to hold one of these values, something like:
C++
double salaryAndBonus;
salaryAndBonus = monthlySalary + (monthlyBonus / 100 * monthlySalary);
payCheck = salaryAndBonus - (taxes / 100 * salaryAndBonus);

The above assumes that the taxes are based on the total of salary + bonus.
   
Comments
nmeyer1 21-Dec-21 9:51am
   
thank you for your assistance, i understand where my error was.
Stefan_Lang 22-Dec-21 7:40am
   
Have a 5. But if I were to write that code I'd add a paycheck*=100; somewhere ;-p
Richard MacCutchan 22-Dec-21 8:03am
   
Thanks. I am sure that you would not write such 'newbie' code in the first place.
You are modifying your monthlysalary in this line
monthlySalary = monthlySalary + (monthlyBonus / 100 * monthlySalary);

If you want to perform the calculations you can store it in some other variables like,
C++
SalaryWithBonus = monthlySalary + (monthlyBonus / 100 * monthlySalary);
	payCheck = SalaryWithBonus - (taxes / 100 * SalaryWithBonus);

After this your monthly salary wn't be affected
   
Comments
nmeyer1 21-Dec-21 9:35am
   
thank you very much.
Without analyzing your code in any detail, it is sometimes possible to guess what is going wrong by comparing the expected and actual output. They differ by 280 (5880 - 5600), so where could that be coming from? Well, it's suspiciously the monthly salary (5600) times the monthly bonus (5%)...
   
v2
Comments
nmeyer1 21-Dec-21 11:06am
   
thank you for your time
Your code has some bad design by not using some class design. When doing this you could have written some test code and so had found the bugs.

starting code example
C++
class Person {
String firstName;
float monthlyBrutto;

bool read(instream file); // read person data

float getMontlyNetto(); // some formula to implement
float getPaycheck(); // some formula to implement

void print(); // output
}
   
Comments
nmeyer1 21-Dec-21 11:08am
   
thank you for your time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900