I have another question. I also read that the variables declared in a function can only stay in the function they are declared in because of scope rules. Does that mean that the calculations done in one function can't be used in another function? I'm really confused though, because when I wrote a function that printed the name, hours worked, and hourly rate inputted by the user in another function, it printed the name, but not the hours worked or the hourly rate (they were printed as 0.00).
So how can I use the info obtained in the userInput() function in another function it if the variables declared in a function are restricted to only the functions they are declared in? I've also tried declaring external variables after declaring the function double calcPay (double hoursWorkedArray[], double hourlyRateArray[]) and before the { brace , but I got an error when I tried to do that.
Code:
void userInput(char nameArray[][30], double hoursWorkedArray[], double hourlyRateArray[])
{
int i;
for (i=0; i<5; i++)
{
printf("Enter name.\n");
scanf("%s", nameArray[i]);
printf("How many hours did they work this week?\n");
scanf("%lf", &hoursWorkedArray[i]);
printf("What is their hourly rate?\n");
scanf("%lf", &hourlyRateArray[i]);
}
}
// calculates base,gross,overtime, and net pay
double calcPay (double hoursWorkedArray[], double hourlyRateArray[])
{
double basePay[5], overTime[5], grossPay[5], netPay[5];
int i;
for (i=0; i<5; i++)
{
if (hoursWorkedArray[i] > 40)
{
basePay[i] = hourlyRateArray[i] * hoursWorkedArray[i];
overTime[i] = (hoursWorkedArray[i] - 40) * 1.5 * hourlyRateArray[i];
grossPay[i] = (hourlyRateArray[i]*40) + overTime[i];
netPay[i] = (hourlyRateArray[i]*40) + overTime[i] - ((hourlyRateArray[i]*40) + overTime[i])*.2;
}
else
{
basePay[i] = hourlyRateArray[i] * hoursWorkedArray[i];
//taxesOwed[i] = (hourlyRateArray[i]*40) * .2;
grossPay[i] = (hourlyRateArray[i]*40) * .8;
overTime[i] = 0;
netPay[i] = (hourlyRateArray[i]*40) + overTime[i] - ((hourlyRateArray[i]*40) + overTime[i])*.2;
return basePay[i], grossPay[i], overTime[i], netPay[i];
}
}
}