Thread: question about using returned values

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    question about using returned values

    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("&#37;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];
    
    			}
    		}
    	
    
    }
    Last edited by pokiepo; 07-01-2008 at 05:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Question about assigning values
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 07:36 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  5. Question about ignoring values
    By jaisch in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2005, 12:06 AM