Reputation: 69
hi i just started to learn c++. I'm trying to figure out how to make a variable change depending on another variable. It shows a strange answer when i try it idk where its getting it from. anyways i'll just put my code in here and hope someone understands what i am doing.
#include <iostream>
long int mult (float hours,float payrate,float tax,float overtimerate,float overtimehours,float overtimeday,float insurance);
int main()
{
float insurance;
float hours;
float payrate;
float tax;
float overtimerate;
float overtimehours;
float overtimeday;
insurance=.81;
overtimehours=0;
overtimerate=1.5;
tax=4.93372123545367;
std::cout<<"Paycheck calculator\n";
std::cout<<"Enter your hours\n";
std::cin>>hours;
std::cin.ignore();
std::cout<<"Did you work over 8 hours in a day?\n If so enter how much? If none enter 0\n";
std::cin>>overtimeday;
if (hours>80)
{
overtimehours=((hours - 80)+overtimeday);
}
std::cout<<overtimehours+overtimeday<<"\n";
std::cout<<"Enter your pay rate\n";
std::cin>>payrate;
std::cin.ignore();
std::cout<<"Your paycheck should be approximately: "<<((overtimeday+overtimehours)*(overtimerate*payrate
))+(((hours-overtimehours)*payrate)-((((hours-(overtimeday+overtimehours))*payrate)/tax)
-insurance))<<"\n";
}
Upvotes: 1
Views: 310
Reputation: 7985
Other answers pointed out very serious flaws in your code, so you should take them seriously, but there is one thing, the topic suggests you are struggling with, that wasn't explained. Let me try.
I think you are struggling with a way to calculate overtime
. You take an input from a user, that indicates the number of hours
of work. If that number is less or equal to 80
, there is no overtime. Otherwise the overtime is equal to hours - 80
, right?
You can express the logic in C++ like this:
float hours;
float overtime;
//....
cout<<"enter the amount of hours you have worked\n";
cin>>hours;
if(hours > 80)
{
overtime = hours - 80;
}
else
{
overtime = 0;
}
If you initialize the overtime
variable to 0
, you can omit the else
part.
float hours;
float overtime = 0;
//....
cout<<"enter the amount of hours you have worked\n";
cin>>hours;
if(hours > 80)
{
overtime = hours - 80;
}
Upvotes: 1
Reputation: 64682
In the expression:
x=(t*w)+((hours-t)*rate);
You are using t
, even though t
has never been set.
t
will be undefined at this point, and may have any value.
Same thing with hours
and rate
.
Your compiler should have given you warnings about uninitialized variables.
Edit looking at your code more, it seems you are attempting to establish "rules" for hour, rate and t, then later fill in the values, expecting those "rules" will be preserved.
C++ does not work like this. It is a sequential language, meaning that each instruction is executed once, as it is read, top-to-bottom.
You need to change your program flow to be:
It seems you reversed steps 1 and 2, trying to setup the calculations before gathering the variables.
Upvotes: 9
Reputation: 18954
I see you have a function declared:
int mult (float x,float t,float w,float hours, float rate,float z);
that you never do anything with. I think the code you have in main
mostly belongs in mult
and that your main should either assign values to these variables or prompt the user for them and read them from cin
. You do this for some variables later on, but then never use what you read in.
I agree with @quantumSoup btw, your variables are poorly named. Also, your code is really in an odd order. This makes it hard to guess what you want.
Upvotes: 1
Reputation: 36082
you haven't initialized hours (it is always good practice to initialize all your local variables)
note also that 'hours' is of type float and these are not represented exactly so your compare with 80 may yield a different result than you expected.
Upvotes: 2
Reputation: 98469
You need to assign a value to t
before using it. Saying float t;
will not actually give it a meaningful value. Ditto hours
and rate
. Assign them before doing x=(t*w)+((hours-t)*rate);
.
Upvotes: 2