using values from other functions and parameter passing help

This is a discussion on using values from other functions and parameter passing help within the C Programming forums, part of the General Programming Boards category; So I've been trying to figure out how to take values from different functions to use in other functions, but ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    using values from other functions and parameter passing help

    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:
    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("&#37;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;
    }
    Here's what the output came out to be like when I tested the program:
    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
    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?
    Last edited by pokiepo; 07-01-2008 at 04:57 PM. Reason: used an old version of the code before

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    Code:
    scanf("%f", &hoursWorkedArray[i]);
    scanf("%f", &hourlyRateArray[i]);
    You're reading into doubles, but %f (in scanf()) means float; you instead want to use %lf. Don't use %lf in printf(), though. Due to the rules of C, %f is used for both floats and doubles in printf().

    Your compiler should be able to catch errors like this. If you're using gcc, always build with the -Wall option, which turns on a lot of helpful warnings. Check your compiler's documentation if you don't use gcc. Compiler warnings are immensely useful.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also pay attention to code like

    Code:
    		printf(" Base pay: $&#37;.2f\n Gross pay: $%.2f\n Gross pay: $%.2f\n Net pay: $%.2f\n Taxes paid: $%.2f\n", calcPay(hoursWorkedArray, hourlyRateArray));
    format has 5 format specifiers - and you provide only one additional parameter. If you are lucky - you just print garbage, in the worst case you just ask for crash
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arrays to Functions...
    By txcs in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 10:36 AM
  2. Passing Functor as template parameter
    By MarkZWEERS in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2008, 08:13 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 01:58 PM

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