Reputation: 3
I'm writing a program that will display an employee's information (pre-defined), i.e., first name, last name, age, id number, gross salary, tax amount and net salary after entering an id number and the amount of hours worked. Pretty basic yea?
My problem is that I can't get the compiler to perform the right calculations to give me gross salary, tax amount and net salary after accepting the work hours.
Based on what I'm seeing the problem is that the main variable involved in these calculations, i.e., totalHoursWorked
is based on user input.
If I declare that same variable and assign it an amount, then the calculations run smoothly and output the correct value, but that's a problem because the user needs to be able to enter any value from .1
to 40
(signifying a maximum of 40 work hours).
However, the moment I remove the assigned value and ask for user input, everything goes wrong.
Below I've written the part of the code I'm having problems with. It was just a rough draft to help me visualize where the problem was and focus on tweaking it:
#include <iostream>
using namespace std;
int main()
{
double hourlyRate=7.5;
double taxPercentage=0.16;
double totalWorkHours;
double grossSalary=hourlyRate*totalWorkHours;
double taxAmount=grossSalary*taxPercentage;
double netSalary=grossSalary-taxAmount;
cout<<"Please enter total work hours"<<endl;
cin>>totalWorkHours;
cout<<grossSalary <<endl;
cout<<taxAmount <<endl;
cout<<netSalary <<endl;
system ("pause");
return 0;
}
This is the output I get:
Please enter total work hours
40 <----i input 40 here
3.95253e-323 <--- These are the results of the calculations
4.94066e-324
3.45846e-323
Press any key to continue..._
Upvotes: 0
Views: 92
Reputation: 132
Im not familiar with c++. I started out with it but my classes are all java so..yeah its been awhile. Any who this is what I would do.
double hourlyRate=7.5;
double taxPercentage=0.16;
double totalWorkHours;
double grossSalary;
double taxAmount;
double netSalary;
cout<<"Please enter total work hours"<<endl;
cin>>totalWorkHours;
grossSalary = hourlyRate*totalWorkHours;
cout<<grossSalary <<endl;
taxAmount = grossSalary*taxPercentage;
cout<<taxAmount <<endl;
netSalary = grossSalary-taxAmount;
cout<<netSalary <<endl;
system ("pause");
return 0;
Thats my two cents.
Upvotes: 0
Reputation: 4196
I think the actual problem in OP's thinking is that they see C++ as a sort of symbolic mathematical notation. I.e., define the relationships with those first three formulas, and then pass a concrete value to update the result, as per the formulas. However, this is not true. What the posted code does, is to calculate the values of grossSalary
, taxAmount
and netSalary
based on the value of totalWorkHours
, which as already said is undefined and garbage, and then assign a new value to totalWorkHours
, which does not affect the already performed calculations.
Upvotes: 1
Reputation: 99
Try placing your calculations after the user has inputed their work hours. Define totalWorkHours
and then ask them for an input and then define all of the other variables with their calculations. Should work!
Upvotes: 0
Reputation: 99094
Try something simpler:
int A = 1;
int B = A + 1;
A = 3;
cout << B << endl;
The result is 2
.
Statements in C++ are not like simultaneous equations in mathematics, they are assignments, and they can be eclipsed by later assignments. The statement:
int B = A + 1;
is equivalent to:
int B;
B = A + 1;
You have set the value of B
to 2, and the assignment is over. You can then set the value of A
to whatever you like and it will have no effect on B.
Upvotes: 0
Reputation: 1476
You're doing your calculations before you've read in totalWorkHours
, so the calculations are based on whatever random value happened to be in memory at that location.
Upvotes: 0