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.
Printable View
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.
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.Quote:
What do you see in the program that would have to be changed to get it to work?
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?
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:
payroll ought to be declared as an array if that is correct usage of 'payroll'.Code:double payroll;
int i = 0;
double whatever = /* .. */
payroll[i] = whatever;
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.
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 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.Code:int Add( int op1, int op2 )
{
return op1 + op2;
}
For example:
I can call the function from main as
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, 19 ); /*Or any other combinations of two numbers, for that matter*/
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.Code:int main( void )
{
int sum;
sum = Add( 12 );
return 0;
}
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.
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.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;
}
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.
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.
Look at your function:
You are trying to return a value from a void function. You should be defining this function to return a float.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 */
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:
Another problem is comment styles. In strict C90 and earlier, only /* comments */ are allowed. The C++ styleCode: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;
}
// 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.
Good point jimblumberg and whiteflags, Thank you. I'll continue to modify it and reply when I have another struggle.
I took the help from previous post and restructured the payroll program with reorganized functions now it works. Thank you everyone.