# Thread: Beginner that needs lots of help, Program that calculates employee data

1. ## Beginner that needs lots of help, Program that calculates employee data

A warning, I am a super beginner and I am having great difficulty and frustration trying to write this program I need to do for school. Any help will be greatly appreciated.
I need to write a program that asks for user input, employee name, hours worked, and hourly rate.
Write a method that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference.

Write a method that calculates the gross, base and overtime pay, pass by reference.

Write a method that calculates tax, taking as input the gross pay, returning the tax owed.

Calculate the total amount paid (gross pay) for all 5 people. Use the return value from the method that calculates the gross pay.

Write a print method that generates the output, including the total amount paid, in addition to the data for each employee.

So far I have this:
Code:
```#include <stdio.h>#include <stdlib.h>

int employeedata(char* ch, float* x, float *y)
{
scanf_s("%s", *ch);
scanf_s("%f", *x);
printf("Enter number of hours worked:\n");
scanf_s("%f",*y);
}
int calculatepay(float*a, float*b, float*c)
{
if (b > 40) {
printf("Base pay: %.2f\n", *a * 40);
printf("Overtime pay: %.2f\n", (*b - 40)*(*c * *a ));
printf("Your paycheck is: %.2f\n", ((*b - 40)*(*a * *c)) + (40 * *b));
printf("Taxes paid:%.2f\n", .2*((*b - 40)*(*a * *c) + (40 * *a)));
printf("Money taken home: %.2f\n", (*b - 40)*(*a * *c) + (40 * *a) - (.2*((*b - 40)*(*a * *c) + (40 * *a))));
}
else {
printf("Your paycheck is: %.2f\n", (*a * *b));
printf("Taxes paid: %.2f\n", .2*(*a * *b));
printf("Money taken home: %.2f\n", (*a * *b) - (.2*(*a * *b)));
}
printf("\n");
}

int main()
{
//variables
float rate[5], hours[5];
float overtimerate = 1.5;
char name[5][20];

int loop;

//loop 5 times
for (loop = 0; loop < 5; loop++)
{
//call function
employeedata(&name[loop], &rate[loop], &hours[loop]);
//use if to break out of loop after input if -1 is entered
if (strcmp(name, "-1") == 0)
break;

if (rate[loop] == -1)
break;

if (hours[loop] == -1)
break;

}
//output
//math calculations to calculate pay and taxes
//differentiate between overtime and non overtime pay
for (loop = 0; loop < 5; loop++){
printf("Name: %s\n", name[loop]);
printf("Hourly rate: %.2f\n", rate[loop]);
printf("Hours worked: %.2f\n", hours[loop]);
int calculatepay(&rate[loop], &hours[loop], &overtimerate);
}

system("pause");

return 0;
}```
I have no idea how write a function that uses values from another function. Can someone explain to me how to do that? Also I have no idea how to fix my employeedata function. I get this error after inputting the first name:
Exception thrown at 0x0F87FB93 (ucrtbased.dll) in Project6.exe: 0xC0000005: Access violation writing location 0x004F0000.

If there is a handler for this exception, the program may be safely continued.

2. According to me instead of writing the complete program you can start checking if employeedata function alone is working without looping it for 5 times. Even before that writing a single line for scanf_s and reading and printing will help.

3. Does that code even compile, without warnings or errors?

Here is what my compiler reports for your program:
main.c||In function ‘employeedata’:|
main.c|9|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|11|warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]|
main.c|13|warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]|
main.c||In function ‘calculatepay’:|
main.c|17|error: comparison between pointer and integer|
main.c||In function ‘main’:|
main.c|50|error: passing argument 1 of ‘employeedata’ from incompatible pointer type|
main.c|6|note: expected ‘char *’ but argument is of type ‘char (*)[20]’|
main.c|52|error: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]|
main.c|71|error: expected declaration specifiers or ‘...’ before ‘&’ token|
main.c|71|error: expected declaration specifiers or ‘...’ before ‘&’ token|
main.c|71|error: expected declaration specifiers or ‘...’ before ‘&’ token|
main.c|39|warning: unused variable ‘overtimerate’ [-Wunused-variable]|
main.c||In function ‘employeedata’:|
main.c|14|warning: control reaches end of non-void function [-Wreturn-type]|
main.c||In function ‘calculatepay’:|
main.c|30|warning: control reaches end of non-void function [-Wreturn-type]|
If there is a handler for this exception, the program may be safely continued.
No, you need to fix the code so it doesn't crash.

Jim

4. Originally Posted by Satya
According to me instead of writing the complete program you can start checking if employeedata function alone is working without looping it for 5 times. Even before that writing a single line for scanf_s and reading and printing will help.
I used your previous advice and I've experimented a lot of things to fix my employeedata function alone but I keep getting errors. The thing is it worked when my friend used it in linux, but when I use it in visual studio it keeps giving me errors.

5. You also need to heed what Jim said.

scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
scanf_s with a %s format needs a length parameter, which you don't even specify.

6. Just to add, do not use System OS calls to pause the console.
Use getchar() to wait for a keyboard press (such as Enter) to
quit the program.

The thing is it worked when my friend used it in linux, but when I use it in visual studio it keeps giving me errors.
Not 100% sure here, but System() is a Windows function, so Linux would not understand it.
Also, I won't debate this, but it's probably not wise to use Microsoft's *cough* standard safe *cough*
C functions. If you ever write code that needs to be ported to other platforms and re-compiled for some
reason then it will cause conflicts. Of course, if you are being taught that way then fair enough

7. Not 100% sure here, but System() is a Windows function,
No, system() is a standard C function. However the parameters may not be valid with all operating systems. For example Linux doesn't have a "pause" program.

Also, I won't debate this, but it's probably not wise to use Microsoft's *cough* standard safe *cough*
C functions.
I agree, however these "safe" functions have been added to the C 11 standard as optional features. However most compilers, at this time, do not have support for these optional features. And since the Microsoft C compiler doesn't support C11 there is no guarantee that their functions actually completely conform to the C11 standard.

Jim