3 Replies - 301 Views - Last Post: 05 December 2010 - 12:54 PM Rate Topic: -----

#1 hectc7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-December 09

Functions

Posted 05 December 2010 - 11:00 AM

We are studying functions in our C++ class. I was asked to make a program that allowed the user to calculate their weekly rate pay. They would be able to input the number of hours worked and then their hourly pay... if the hours worked were > 40... 150% Overtime would need to be added. I can get the functions to talk to each other just fine. There were to be 4 functions and a MAIN(). Here is what I have so far.
 #include <iostream>
using namespace std;

//function prototype//
int readHours ();
int payRate();
int overTime();
void display();

// main function //
int main ()
{
	int hoursReturn = 0;
	int payReturn = 0;
	int overTimeReturn = 0;





	hoursReturn = readHours ();
	payReturn = payRate ();
	overTimeReturn = overTime ();

	


	system ("pause");
	return 0;

}

// function definitions//

int readHours ()
{
	int hours = 0;
	int hoursReturn = 0;

cout << " enter the hours worked for the week"<< endl;
cin>> hours;

return hoursReturn;

}

int payRate ()
{
	int pay = 0 ;
	int payReturn = 0;

	cout << " now enter the pay rate (per hour)" <<endl;
	cin>> pay ;

	return payReturn;
}

int overTime ()
{
	int hours = 0 ;
	int overTimeReturn = 0;
	int hoursReturn = 0 ;

hoursReturn = hours;

    	if ( hours  > 40 )

		cout << " you have recieved overtime " << endl;
	else
		cout << "no overtime"<<endl;

	return overTimeReturn;

}

int display ()

{
	int displayReturn = 0;
	int overTimeReturn = 0;
	 
} 



I cannot for the life of me ge the program to even say that it received overtime.... always says "no overtime" ... I know that the return value of the hours input should go to the function that will decide ( with an "if" statement weather it will, but I have tried. Can anyone help?

Is This A Good Question/Topic? 0
  • +

Replies To: Functions

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: Functions

Posted 05 December 2010 - 11:55 AM

Let's start with your readHours function. You store the user response in the variable:
hours
yet you return the variable:
hoursReturn
making no attempt to validate and copy the user response to this variable.

Do you think that should work?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Functions

Posted 05 December 2010 - 12:01 PM

You will need to pass the "hours" that the person worked into your function overtime().

The way hours is defined now in overtime() it will always be zero.

int overTime ()
{
	int hours = 0 ;



See this link for information on how to use parameters with functions: Functions I

Jim
Was This Post Helpful? 0
  • +
  • -

#4 hectc7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-December 09

Re: Functions

Posted 05 December 2010 - 12:54 PM

I knew that something was wrong... I kept initializing things to 0! of course it was going to keep saying no overtime if my "if" statement said that the variable > 40 = overtime.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1