# Exercice - Calculate the fee

This is a discussion on Exercice - Calculate the fee within the C++ Programming forums, part of the General Programming Boards category; This is just an exercise. And i need to complete this. This is the question : Write a program in ...

1. ## Exercice - Calculate the fee

This is just an exercise.
And i need to complete this.

This is the question :

Write a program in C++ that calculates the amount to be paid to an employee based on the hours worked and rate per hour. A user will enter hours worked and rate per hour for an employee. Your program should have a function payCheck that calculates and returns the amount to be paid. The formula for calculating the salary amount is as follows: for the first 40 hours, the rate is the given rate (the rate that a user has entered); for hours over 40, the rate is 1.5 times the given rate.

i had write my own coding, but it keeps error. it is okay.
Still running, but anyone can help me?
So, this is my coding.
The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.

Code:
```#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std;

int main(void)
{
char *empname, *test, *final_output;
float hours=0, pay_rate=0, gross=0, net=0;

//loop
do {
/*-- get first & last name --*/
cin>>empname;

/*--
get pay rate with error checking on
null and 0 values
--*/
do {
cout << "Pay Rate: ";
cin >> test;
if (test == NULL)
cout << "Invalid pay rate.";
else if(atof(test) != 0)
break;
else
cout << "Invalid pay rate.";
} while (1);
pay_rate=atof(test);

/*--
get hours worked with error checking on
null and 0 values
--*/
do {
cout << "Hours Worked: ";
cin >> test;
if (test == NULL)
cout << "Invalid amount of hours.";
else if(atof(test) != 0)
break;
else
cout << "Invalid amount of hours.";
} while (1);
hours=atof(test);

/*-- calculate values --*/
gross=hours*pay_rate;

/*-- set out precision --*/
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);

/*-- create output --*/
sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
empname);
cout << final_output;
cout << "Gross: " << gross << endl;

/*-- do another? --*/
cout << "Calculate Another? (y/n)?";
cin >> test;

/*-- check first value of string for y/Y --*/
} while (!strncasecmp(test,"y",1));

return 0;
}```

3. This is my latest code
It does not have an error, but i cannot calculate the rate of overtime 4O hours.
i am still running my exercise and need help.

Code:
```//Write a program in C++ that calculates the amount to be paid
//to an employee based on the hours worked and rate per hour.
//A user will enter hours worked and rate per hour for an employee.
//Your program should have a function payCheck that calculates and
//returns the amount to be paid. The formula for calculating
//the salary amount is as follows: for the first 40 hours,
//the rate is the given rate (the rate that a user has entered);
//for hours over 40, the rate is 1.5 times the given rate.

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std;

int main(void)
{
char *empname, *test, *final_output;
float hours=0, pay_rate=0, emprate=0;
float pay_rate_overtime=1.5;

//variable
final_output = new char[1024];
empname = new char[50];
test = new char[10];

//loop
do {
cin >> empname;

do {
cout << "\nEnter Your Pay Rate : ";
cin >> test;
if (test == NULL)
cout << "Invalid pay rate.";
else if(atof(test) != 0)
break;
else
cout << "please enter digit only";
}
while (1);
pay_rate=atof(test);

do {
cout << "\nEnter Your Worked Hours: ";
cin >> test;
if (test == NULL)
cout << "please enter digit only";
else if(atof(test) !=  0)
break;
else
cout << "Invalid amount of hours.";
}
while (1);
hours=atof(test);

/*-- calculate values --*/
emprate=hours*pay_rate;

/*-- set out precision --*/
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);

/*-- create output --*/
sprintf(final_output,"\n\n Your Pay Rate is : \n------------------------------\n", empname);
cout << final_output;
cout << "Your Salary Is : " << emprate <<endl;

/*-- do another? --*/
cout << "\n\n Calculate Another? (y/n)?";
cin >> test;

/*-- check first value of string for y/Y --*/
} while (!strncasecmp(test,"y",1));

/*-- return memory to the heap --*/
delete [] empname;
delete [] test;
return 0;
}```

Code:
```// you are really in need of the new compiler

#include <cstdlib> //this is the c++ equivalent of C include #include <stdlib.h>
//do you need this??  #include <stddef.h>
#include <cstring> //see above #include <string.h>
//#include <stdio.h> - should not be needed in C++
#include <iostream> //#include <iostream.h> - this incluude is WAAAY outdated

using namespace std;

int main(void)
{
char *empname, *test, *final_output; // YOY - you really need to read about std::string
float hours=0, pay_rate=0, gross=0, net=0;

//loop
do {
/*-- get first & last name --*/
cin>>empname; // not gonna work - use std::string

/*--
get pay rate with error checking on
null and 0 values
--*/
do {
cout << "Pay Rate: ";
cin >> test; //see above
if (test == NULL) // No - not gonna work
cout << "Invalid pay rate.";
else if(atof(test) != 0) // Hey! cin knows how to read floats, you know...
break;
else
cout << "Invalid pay rate.";
} while (1);
pay_rate=atof(test); // see commant above

/*--
get hours worked with error checking on
null and 0 values
--*/
do { // I'm not gonna reapeat myself
cout << "Hours Worked: ";
cin >> test;
if (test == NULL)
cout << "Invalid amount of hours.";
else if(atof(test) != 0)
break;
else
cout << "Invalid amount of hours.";
} while (1);
hours=atof(test);

/*-- calculate values --*/
gross=hours*pay_rate;

/*-- set out precision --*/
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);

/*-- create output --*/
sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
empname); //Hey - this is C++ - use streams
cout << final_output;
cout << "Gross: " << gross << endl;

/*-- do another? --*/
cout << "Calculate Another? (y/n)?";
cin >> test; //not gonna work

/*-- check first value of string for y/Y --*/
} while (!strncasecmp(test,"y",1)); // using std::string you'll get it way simplier

return 0;
}```

5. Code:
` char *empname, *test, *final_output;`
These variables are basically useless because they don't have any space for strings. You really should be using std::string, but if you can't, stay away from pointers if you haven't learned them.

Code:
```sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n", empname);
cout << final_output;```
There aren't enough strings in the sprintf() call to match all of the %s's in the format string. Not that you should be using sprintf() anyway. It's entirely not necessary.

The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.
Yeah you basically didn't do that part. You need to find out if someone worked more than 40 hours, and if so, the income formula is
(40.0f * rate) + ((hours - 40.0f) * rate * 1.5f)
Otherwise, the income formula that you already programmed will work.

Code:
```cout << "Pay Rate: ";
cin >> test;```
Didn't they teach you to write cin >> pay_rate; instead?

Code:
```cout << "Hours Worked: ";
cin >> test;```
And cin >> hours; ?

6. years ago had to do the same exercise
here is a "simplistic" approach.
on my laptop it won't stay open long enough
to verify results

I am using Dev C++ and Windows 8

Code:
```#include <iostream>

using namespace std ;

void pay_me()

{
float hours, rate, pay ;

cout <<"How many Hours did you Work this Fortnight? " ;
cin >> hours ;

cout <<"What is your Rate of Pay? " ;
cin >> rate ;

if (hours > 40)
{
pay = (hours * rate) + (hours - 40) * rate * 1.5 ;
}
else
{
pay = hours * rate ;
}
cout<<"You will be paid \$" << pay ;

}

int main()
{
pay_me() ;
}```

7. Originally Posted by BitRusty
on my laptop it won't stay open long enough
FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com

8. Originally Posted by BitRusty
years ago had to do the same exercise
here is a "simplistic" approach.
on my laptop it won't stay open long enough
to verify results

I am using Dev C++ and Windows 8

pay = (hours * rate) + (hours - 40) * rate * 1.5 ;
Years ago, you shouldn't have used hours for the amount paid at a normal rate like you did here. That will overpay.

9. now running the program/s from inside the IDE, now staying open to see results and able to adjust code accordingly. previously running program from the resident folder

10. [code]
if (hours > 40)
{
pay = (40 * rate) + (hours - 40) * rate * 1.5 ;
[\code]

now it shouldn't over pay