Hello everyone, we are learning functions and I am trying to do some review work. The question is to write the function and definition. I am writing the whole code to get a better understanding, my problem is in the function I am not sure if my, "if" stmts will work.
#include <iostream>
usingnamespace std;
// Function declaration (prototype)
double pay(int hrs, double rate);
int main()
{
int num, result,h,r;
cout<<"Enter hours:";
cin>>h;
cout<<"Enter rate:";
cin>>r;
result = pay(h,r);
cout << "Total pay is" << result << endl;
return 0;
}
// declare pay(. . .) here as well as at the top
{
int underEqual40, over40, total; //if these are to be used in a <<double>> return value,
// shouldn't they be double as well?
if (hrs<=40)
cin>>underEqual40; // because no brackets, this is the only line that is conditional.
underEqual40=hrs*10; //This one will act regardless of the if condition
// 10 is a magic number, <<double rate>> won't have any effect on thes calculation
if(hrs>40);
cin>>over40;//because of << ; >> in the previous line, all the rest of the lines will execute
over40 = hrs-40*(1.5*rate)+hrs*10; // you seem to only use rate in this calculation; is that what you intend?
total = underEqual40 || over40; // error here because you don't want a boolean result
// what are you going to do with << total >> ?
return pay(underEqual40,over40); } // creates a recursive infinite loop by calling iteslf
// assuming the above is what you mean to do by calling pay(h,r)
it's supposed to be inside parenthesis operation first, then left to right.
Not quite. Yes, what inside the parens is evaluated first, but multiplication has higher priority than addition or subtraction. See the table here (bottom of the page): http://www.cplusplus.com/doc/tutorial/operators/
The net effect of what you had was that the rate was multiplied by 1.5, then multiplied by 40, then subtracted from hrs.