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

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    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)
    {
    	printf("Enter your name:\n");
    	scanf_s("%s", *ch);
    	printf("Enter your hourly rate:\n");
    	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. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    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. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,424
    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. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    Quote Originally Posted by Satya View Post
    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. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    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
    Double Helix STL

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,424
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-01-2012, 01:17 AM
  2. Help with program that calculates confidence interval
    By kgrahora in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 11:45 PM
  3. Replies: 5
    Last Post: 10-11-2006, 02:29 AM
  4. Program that calculates the value of PI
    By noodles in forum C Programming
    Replies: 7
    Last Post: 09-06-2006, 05:20 AM
  5. C++ Program that Calculates average of three scores
    By dccog in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2002, 12:03 AM

Tags for this Thread