Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Can you please explain to me why this C++ program does not want to run. I copied this straight from a text book:

C++
//**************************************************************
//Paycheck program
//This program computes an employee's wages for the week
//**************************************************************

#include <iostream>
using namespace std;

void CalcPay(float, float, float&);

const float max_hours = 40.0; //Maximum normal work hours 
const float overtime = 1.5; //overtime rate factor

int main()
{
float payrate; //emp pay rare
float hours; // hours worked
float wages; // wages earned
int empnum; //emp id no

cout <<"Enter emp no";
cin>>empnum;
cout <<"Enter pay rate";
cin>>payrate;
cout<<"Enter hours worked";
cin>>hours;

CalcPay(payrate, hours, wages);

cout <<"Emp no"<<empnum<<endl<<"pay rate"<<payrate<<endl<<"wages"<<wages<<endl;

return 0;
}
//**************************************************************

void CalcPay(float payrate, float hours, float& wages )
{
if (hours > max_hours)
wages = (max_hours * payrate) + (hours-max_hours) *payrate *overtime;
else
wages= hours * payrate;
}
Posted
Updated 27-May-11 23:53pm
v2
Comments
Sandeep Mewara 28-May-11 5:54am    
What do you mean by "does not want to run"? Any error?
hakz.code 28-May-11 6:35am    
Please be specific!unless you describe the problem well it is tough to get the solution..

1 solution

It does run.
The only thing you need to do is add something to let you see the result, before the window closes: Try putting a breakpoint on the return statement, that would help for testing, or use cin to force another read from the keyboard before the application closes.

BTW: I would change the CalcPay function:
float CalcPay(float payrate, float hours)
   {
   float wages;
   if (hours > max_hours)
      wages = (max_hours * payrate) + (hours-max_hours) *payrate *overtime;
   else
      wages= hours * payrate;
   return wages;
   }
This way it returns the value, instead of modifying a parameter. That means you can use it in calculations:
float allPay = CalcPay(JoesRate, JoesHours) + CalcPay(MikesRate, MikesHours);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900