function troubles again

This is a discussion on function troubles again within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> float ot_pay(int, float[], float[]); float calculate_gross(int, int, float[], float[], float[]); main() { /*Variables*/ int employees, x; float ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    15

    function troubles again

    Code:
    #include <stdio.h>
    
    float ot_pay(int, float[], float[]);
    float calculate_gross(int, int, float[], float[], float[]);
    
    main()
    {
    
    /*Variables*/
    
    int employees, x;
    float hours[30], rate[30], fed_tax[10], net_pay[10], overtime[10];
    
    struct info
    {
    char first[30], last[30];
    
    };
    struct info name[100];
    	
    	
    printf ("*****WELCOME TO THE PAYROLL CALCULATOR*****\n\n");
    
    
    for (x = 1; x <=1; ++x)  /*Loop1 - Get Number of Employees*/
    	{
    			printf("Please enter # of employees(1-10): ");
    			scanf("%i",&employees); 
    			if (employees > 10)
    				{	printf ("Invalid Entry\n");
    					--x;
    				}
    			if (employees < 1)
    				{	printf ("Invalid Entry\n");
    					--x;
    				}
    		
    	}
    
    for (x = 1; x <=employees; ++x)  /*Loop 2 - Get info for employees*/
    	{
    		printf ("\nEnter first and last name of employee #%i: ",x);
    		scanf("%s %s",name[x].first, name[x].last);
    		printf("Enter pay rate for employee #%i: ",x);
    		scanf("%f", &rate[x]);
    		printf("Enter hours worked for employee #%i: ",x);
    		scanf("%f", &hours[x]);
    					if (hours[x]<=0)
    						{	printf ("Invalid Entry\n");
    							--x;
    						}
    					else if(rate[x]<=0)
    						{printf ("Invalid Entry\n");
    							--x;
    						}
    					
    					if(hours[x] > 40)
    						{ot_pay(x, hours, rate); /*Call OT function if necessary*/
    						}
    				
    	}	
    
    calculate_gross(x, employees, hours, rate, overtime);
    	
    }
    
    float ot_pay(int x,float hours[],float rate[])
    {	
    	float overtime[10];
    	overtime[x] = (hours[x]-40)*rate[x]*1.5;
    	printf("OT $%.2f\n\n",overtime[x]);
    	return overtime[x];
    }	
    
    
    float calcualte_gross(int x, int employees, float hours[], float rate[], float overtime[])
    {
    for (x = 1; x <= employees; ++x)
    		{	
    			float gross_pay[30];
    	 		gross_pay[x]=hours[x]*rate[x]+overtime[x]/3;
    			return gross_pay[x];
    			
    		}
    }
    *undefined reference to calculate_gross* in red

    Dont know why this is not working. Any advice???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Once again, carefully compare your function declaration with its definition:
    Code:
    float calculate_gross(int, int, float[], float[], float[]);
    Code:
    float calcualte_gross(int x, int employees, float hours[], float rate[], float overtime[])
    One thing you can do to avoid this is to write out the function prototype in full, and then copy it for writing the function definition, or vice versa. I would then expect this as the prototype:
    Code:
    float calculate_gross(int x, int employees, float hours[], float rate[], float overtime[]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    15
    ok well im just trying to understand this.


    I thought when i do the prototype it just something like:
    Code:
    float name(int, float[], float[]);
    then i thought when u call it its
    Code:
    name(variable1, variable2, variable3);
    then when u are defining it you:
    Code:
    float name(variable1, float variable2[], float variable3[]) 
    {
    
    }
    return
    is this right??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wwwildbill View Post
    ok well im just trying to understand this.


    I thought when i do the prototype it just something like:
    Code:
    float name(int, float[], float[]);
    then i thought when u call it its
    Code:
    name(variable1, variable2, variable3);
    then when u are defining it you:
    Code:
    float name(variable1, float variable2[], float variable3[]) 
    {
    
    }
    return
    is this right??
    Not quite (you still need a type on variable1). You can drop the names in the prototype; you shouldn't, but you can.

    You also need to spell "calculate" correctly three times in a row.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21