Thread: Payroll program using Arrays and functions

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    Payroll program using Arrays and functions

    I am having trouble with a payroll program working with arrays and functions. What do you see in the program that would have to be changed to get it to work? File is attached.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    What do you see in the program that would have to be changed to get it to work?
    For one, there's no function ot() defined, but you're passing its return value as an argument to printf. I'd try to compile and see what other not so obvious warnings/errors the compiler throws.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    payroll program, functions, arrays

    I declaired ot a functions and reduced the errors to nine. I have a attached image to show what the errors are. Other than that for now I am trying to figure out a way to make this program work. Any other pointers, things that stick out or ideas that would make this program work?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well a majority of the errors you are getting should be self-explanatory.

    If we start with the first:

    >> 88 error: too few arguments to function 'ClockHours'

    The number corresponds to the line number on the left margin, where the compiler thinks the error is. The text of the error, "too few arguments" is saying that you are calling ClockHours without supplying values for its parameters. A number of syntactical errors could cause this as well, like typing a period for a comma. Just scan over your file, specifically where you call ClockHours, and try to fix the call statements.

    It may turn out that that error is caused by this error:

    >> 89 error: subscripted value is neither array nor pointer

    This means that you are giving a normal data variable a subscript (as in [index]) where it is not appropriate. You should be using the variable as it can be used. If it needs to be used like an array, you need to make appropriate changes for that to be right.

    Just as this will generate the same error:
    Code:
    double payroll;
    int i = 0;
    double whatever = /* .. */
    payroll[i] = whatever;
    payroll ought to be declared as an array if that is correct usage of 'payroll'.

    At that point, after fixing those two things, I'd try to compile again. The process repeats until the build log is empty.

    This helpful programming tutorial brought to you by Apply Your Brain, Inc.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    reply

    I would need some more example code to guide me in the right direction, the description is good but I would need some more example code to help me better to figure this out. I have been struggling with understanding functions.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    int Add( int op1, int op2 )
    {
            return op1 + op2;
    }
    This is an example of a simple function named "Add". It has two parameters: op1 and op2. When I call this function (say, from main() or another function) I must supply it two arguments. These arguments correspond to the parameters used in the function declaration/definition.

    For example:
    I can call the function from main as
    Code:
    int main( void )
    {
            int sum;
    
            sum = Add( 12, 19 ); /*Or any other combinations of two numbers, for that matter*/
    
            return 0;
    }
    If I do not supply the function with enough arguments (in the above example, the arguments were 12 and 19) then the compiler will give me an error.

    Code:
    int main( void )
    {
            int sum;
    
            sum = Add( 12 );
    
            return 0;
    }
    This code will give me an error as I supplied only one argument to the function "Add" but it's defined as taking two.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tiger6425 View Post
    I would need some more example code to guide me in the right direction, the description is good but I would need some more example code to help me better to figure this out. I have been struggling with understanding functions.
    This forum, and every C program on the web, is heavily populated with one or more functions, in a C program.

    Arrays are always a little difficult, since they degrade into just a pointer, when passed to a function. Yes, that's a good thing, but it is always a surprise and slightly confusing, at first.

    Code:
    #include <stdio.h>
    
    int myCountingFunction(char *array); //the function prototype
    
    int main(void) {
      int charNumber;
      char array[] = "One of these mornings, bright and fair";
    
      /* array is the unalterable address of the first element of the array.
          That is what I send to the function - just the array address
    
          charNumber is the int that is returned from the function.
      */
      charNumber = myCountingFunction(array);
    
      printf("\n My array has %d char's in it, not including the end of string char", charNumber);
      return 0;
    }
    
      /* the array was an address, when it was sent. Now, inside this function, it is a full
          blown pointer, so it gets a *right in front of it's name.
      */
    int myCountingFunction(char *array) {  
      int i=0;
      putchar('\n');
      while(array[i]) {     //indices still work
        putchar(array[i]);
        ++i;
      }
       
      return i;
    }
    If you're still having difficulties, come back and tell us EXACTLY what is it that has you stumped. The more specific you are, the more we can help you solve the problem.

    Just saying "My program doesn't work, I need help", doesn't show any understanding on your part, and doesn't help pin-point the problem, either.
    Last edited by Adak; 07-18-2010 at 11:25 PM.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    payroll program, functions, arrays

    I have cleaned some of the errors, now I get this error C2120 'void' illegal with all types. Which I don't know how to fix it. When you think you know you have figured out the problem and think the program is going to work you get 12 errors when you recompile. Can anyone fix this program from here to get this program to work? The files are attached.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at your function:
    Code:
    void CalcGrossPay (float hours, float ot, float Gross_Pay, float Wage)
    {
        int Hours;
    
        /* check for overtime and calculate gross*/
        if (ot > 0) {
    	Gross_Pay = (STD_HOURS * Wage) /* regular pay */
                                 + (ot * Wage);     /* ot pay */
        } /* if */
        else  /* not ot */
        {
           Gross_Pay = Wage * Hours;
        } /* else */
        return(Gross_Pay);
    } /* CalcGrossPay */
    You are trying to return a value from a void function. You should be defining this function to return a float.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Again, it's just about trying to understand your errors. Just from eyeballing the code you can see that some return values are mixed up. With minimal changes I got a clean compile:
    Code:
    float ClockHours (float Hours )
    {
        /*int ClockNumber; unused variable?*/
    
        printf ( "Enter hours worked for clock %6f: ", Hours );
        scanf ( "%f", &Hours );
        return ( Hours );
    }
    
    float CalcGrossPay (float hours, float ot, float Gross_Pay, float Wage)
    ...
    
    int main()
    {
        ...
        return 0;
    }
    Another problem is comment styles. In strict C90 and earlier, only /* comments */ are allowed. The C++ style
    // line comments
    were added in C99. It's mostly a pet peeve of mine, but people should just stop turning to C99 just for comments.

    Try to understand your errors.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    re: payroll program

    Good point jimblumberg and whiteflags, Thank you. I'll continue to modify it and reply when I have another struggle.

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    payroll program, functions, arrays

    I took the help from previous post and restructured the payroll program with reorganized functions now it works. Thank you everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By WellyJ in forum C Programming
    Replies: 6
    Last Post: 03-19-2003, 09:38 AM
  5. some help on a simple payroll program
    By wildstyler in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2002, 05:30 PM