Learning functions

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.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>
using namespace 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;
}




{
   
	int underEqual40, over40, total;
	
	if (hrs<=40) 
	  cin>>underEqual40;
	  	underEqual40=hrs*10;
	
	if(hrs>40);
		cin>>over40;
			over40 = hrs-40*(1.5*rate)+hrs*10;

	total = underEqual40 || over40; // error here
	
   return pay(underEqual40,over40); }
Several problems:

Line 25: You're missing the function header.

Line 38: You're doing a logical or between two numbers. I doubt that is what you want. Not clear what you intended.

Line 40: This is a recursive call. Again, probably not what you intended.

You don't describe what your if statements are supposed to do, so can't tell you if they're correct.

Lines 32,36: Not clear if you intended these statements to be part of your if statements. Your indentation makes this unclear.

Line 38: You're calculating total, but don't do anything with it.

Line 17: You're returning a double from pay(), but assigning it to an int. It's going to get truncated.
Thx for the reply.

Here is the question:
"Write the definitions of the functions whose declarations and descriptions are given below.

double pay (int hrs, double rate);
// Return pay, where hours over 40 are paid at 1.5 times the regular rate."


I'll redo the code and post it again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace 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) 
ok, got it working for under 40, at 50 I am getting -250.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;


double pay(int hrs, double rate); 

int main()
{
   int num, result;
   result = pay(50,15.00);
   cout << "Total pay is: " << result << endl; //output result
   
   	return 0;
}



double pay(int hrs, double rate) 

{
   
	double h1, h2, total,result,r;
	
	if (hrs<=40)
	result = hrs*rate;
	 
	else
	result = hrs-40*(1.5*rate)+40*rate;

	 
	 
	 return result; 
}
Ever hear of operator precedence?

Take a look line 28. Which operation is performed first?
hrs-40 or 40*(1.5*rate)

I did, it's supposed to be inside parenthesis operation first, then left to right.

40*(1.5*rate)?

fixed it using parenthesis.

Thanks!
Last edited on
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.


Last edited on
Thanks!
Topic archived. No new replies allowed.