C++ array issues

I have the code working but it is not what the instructor is wanting. Here is what I am having to do:

Write a C++ console application that asks the user to enter the hourly wage and number of hours worked for each week in a 4-week pay period. Create a custom function with appropriate input parameters and return type that calculates the total gross pay based on the values entered by the user. Use suitable loops for the user input and the processing of data entered by the user. The program should print out the gross pay for the pay period for the employee.

He is wanting an array to pass the information He said "You need a procedural program with a custom function (also called a user-defined function) that calculates the gross pay for the pay period. Remember, the pay period is 4 weeks. This means that you will need to use an array to pass the hours data to the custom function, since the overtime needs to be calculated on a week-by-week basis. To give you an idea, the prototype for the function should be something like:

double calculateGrossPay(float hours[], float rate);"

My code is below on what I have I just do not know how to put in the array he is wanting. Please help I am new at this.


#include <iostream>
#include <iomanip>

using namespace std;

// This is the function that will calculate the salary of the hourly worker.
class EmployeeSalary
{
public:
/* hourly workers*/

double CalculateHourlySalary(double hrRate, double hours)
{
if ( hours <= 40 )

salary = hours * hrRate;

else

salary = 40.0 * hrRate + ( hours - 40 ) * hrRate * 1.5;

return salary;
}





private:
double salary;
};


int main()
{
//Naming the variables so it will be able to hold the information the user is putting in.
EmployeeSalary salary;
double sal;
double hrRate;
double hours;

{


// This is asking for the users input so it will be able to pull the numbers to the function that it is calling for the calculation.

cout<<"Enter Hour Rate:$ ";
cin>>hrRate;
cout<<"Enter hours worked: ";
cin>>hours;
cout<<"Hourly worker's salary is:$ "<<salary.CalculateHourlySalary(hrRate,hours)<<endl;
cout<<endl;
}

return 0;
}
the overtime needs to be calculated on a week-by-week basis

The overtime is not explained anywhere.

You have class EmployeeSalary. Why?
You seem to be still at the arrays and functions.

See:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/


PS. Posted code will look better, if you use code tags. See http://www.cplusplus.com/articles/jEywvCM9/
how would I be able to turn this into an array to pass it through to the function? That is where I am stuck.
Last edited on
a very simple example of doing that:
void foo(int * a)
{
cout << a[1];
}

int main()
{
int array[10];
array[1] = 1911;
foo(array);
}
Last edited on
where would I put

void foo(int * a)
{
cout << a[1];
}

in the code?
Where did jonnin put it in his code?
You do have a function in your code already (even though it is a class member function). Where is it?

It does seem that you did not gain anything by reading the two tutorials that I did give links to. Why should we continue writing anything, if it is useless?


Do not copy-paste (aka "put") anything anywhere. Try to understand and then use that knowledge.
the c++ compiler / parser really only has 1 rule about what code goes where that you need to know about right now. It has to know what something is before you use it, from the top down and when using multiple files, you have those compiled in an orderly fashion.

so
int main()
...
foo()
}

void foo()
{

}

won't work because you used it in main but it is invented after main.
that is why headers and .h files are useful, you can put the header above main and the body below it if you like:
void foo(); //header has a ; on it, otherwise identical to the function's define line (the type name (args) line)

so the answer is that it really does not matter. As you are beginning you may put them all above main and ignore headers for now, but soon you should learn about those. Get it working using no headers, before main, and once you have that, then maybe put it below main with a header for practice.
Last edited on
OK I will see what I can do. I have to use headers because that is what the instructor is wanting.
Topic archived. No new replies allowed.