So I've been trying to figure out how to take values from different functions to use in other functions, but I still can't seem to find the problem with my code.
The purpose of the program below is to gather names, hourly rates, and hours worked for 5 employees and then calculate their taxes owed and types of pay using parameter passing and pointers. Also, I read that the purpose of pointers is to change values, but I don't see how I'd need to change any values in my program to get what I need to get (gather names/hourly info, calculating pay and taxes using the hourly rates/worked info).
edit: 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. Does that mean that the calculations done in one function can't be used in another function? I'm really confused though, because when the program printed the output, it printed the name that was typed in, but not the hours worked or the hourly rate. Also, how can I use the info obtained in the userInput() function in the functions below it if the variables declared in a function are restricted to only the functions they are declared in? I've 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.
here's the full code:
Here's what the output came out to be like when I tested the program:Code:#include <stdio.h> #include <string.h> // gets name and hourlyrate and hours worked from user 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("%f", &hoursWorkedArray[i]); printf("What is their hourly rate?\n"); scanf("%f", &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]; } } } // calculates taxes owed double calcTax (double grossPay[]) { int i; for (i=0; i<5; i++) { double taxPaid[5]; taxPaid[i] = grossPay[i]*.2; return taxPaid[i]; } } //calculates the total amount paid to five emplorees double totalPaid (double grossPay[]) { double totalPaid; totalPaid = grossPay[0] + grossPay[1] + grossPay[2] + grossPay[3] + grossPay[4]; return totalPaid; } void printPay(char nameArray[][30], double hoursWorkedArray[], double hourlyRateArray[]) { int i; for (i=0; i<5; i++) { printf("Pay to %s: \n", nameArray[i]); printf("Hours worked: %.2f\n", *&hoursWorkedArray[i]); printf("Hourly rate: $%.2f\n", *&hourlyRateArray[i]); printf(" Base pay: $%.2f\n Gross pay: $%.2f\n Gross pay: $%.2f\n Net pay: $%.2f\n Taxes paid: $%.2f\n", calcPay(hoursWorkedArray, hourlyRateArray)); } } int main(void) { double hourlyRate[5], hoursWorked[5]; char name[5][30]; userInput(name, hoursWorked, hourlyRate); printPay(name, hoursWorked, hourlyRate); return 0; }
What am I doing wrong? I'm guessing that some of the functions aren't able to get the values that were obtained in other functions?Enter name.
jas
How many hours did they work this week?
50
What is their hourly rate?
20
Enter name.
js
How many hours did they work this week?
20
What is their hourly rate?
20
Enter name.
jsk
How many hours did they work this week?
40
What is their hourly rate?
70
Enter name.
djfh
How many hours did they work this week?
10
What is their hourly rate?
40
Enter name.
jfg
How many hours did they work this week?
60
What is their hourly rate?
40
Pay to jas:
Hours worked: 0.00
Hourly rate: $0.00
Base pay: $0.00
Gross pay: $0.00
Gross pay: $0.00
Net pay: $0.00
Taxes paid: $0.00
Pay to js:
Hours worked: 0.00
Hourly rate: $0.00
Base pay: $0.00
Gross pay: $0.00
Gross pay: $0.00
Net pay: $0.00
Taxes paid: $0.00
Pay to jsk:
Hours worked: 48387319012088127000000000000000000000000000000000 0000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 00000.00
Hourly rate: $0.00
Base pay: $0.00
Gross pay: $0.00
Gross pay: $0.00
Net pay: $0.00
Taxes paid: $0.00
Pay to djfh:
Hours worked: 17374505957214276000000000000000000000000000000000 0000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 0000.00
Hourly rate: $0.00
Base pay: $0.00
Gross pay: $0.00
Gross pay: $0.00
Net pay: $0.00
Taxes paid: $0.00
Pay to jfg:
Hours worked: 0.00
Hourly rate: $1645633314734055200000000000000000000000000000000 00000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 000000000000000000000000000000
00000000000000000000000000000000000000000000000000 0000.00
Base pay: $0.00
Gross pay: $0.00
Gross pay: $0.00
Net pay: $0.00
Taxes paid: $0.00