#include <iostream.h>
float salary (int, float);
int main()
{
int n,h,i;
float r,GS;
cout <<"Enter number of employees: ";
cin >> n;
for (i=0;i<n;i++)
{
cout << "\n Hours= \t";
cin >>h;
cout<< "\n Rate= \t";
cin >> r;
GS=salary(h,r);
cout << "\nSalary=\t"<<GS << "\n";
cout << "Rate=\t"<<r<<"\n\n";
}
return 0;
}

float salary(int h, float r)
{
float a;
if (h > 40)
r=r+ 20% ;
a=h*r;
return(a);
}

The calculation is wrong

Edited 6 Years Ago by frag: n/a

It has no indentation?

It's using old, outdated include files?

It has meaningless variable names?

Oh, and the % symbol is used incorrectly.


You really should explain your problem. What should it do, what it's actually doing, where you think the problem might be. If your compiler generated any error messages, what do they say, what line do they point to?

Edited 6 Years Ago by vmanes: n/a

Hey vmanes

The only thing wrong about it is that I used the % incorrectly.

help me out please?

I can't tell what you're trying to do with this as there are no comments and the variable names aren't specific....but is the

r = r + 20%

supposed to mean r = r + 20(percent of something)?

It's supposed to mean if Hours > 40 then you add +20% on top of the rate.

I think I figured it out it's r=r+(r*20/100)

Thanks everyone for your replies

I think I figured it out it's r=r+(r*20/100)

Yes, that works, but you're skating on thin ice (to get in an Olympics reference ;) )

20/100 by itself will not give you the 0.2 result you want. In your code, the operation is being coerced to floating point division by the fact that r is a float, and is an operand of the first operation carried out.

Why not just multiply by 0.2? Or at least ensure you get what you mean, and mean what you get, by using 20.0 / 100.0?

And as another aside, consider dropping the use of float type and always use doubles. Reserve floats for those cases where saving a lot of memory at the expense of some precision is a worthwhile tradeoff.

This question has already been answered. Start a new discussion instead.