Thread: parameter passing and pointers

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

    parameter passing and pointers

    I wrote another program for homework that calculates 5 employees' weekly pay using parameter passing and pointers, but when I tried to compile the program I got the following errors:
    'userInput' : cannot convert parameter 1 from 'char (*)[5][30]' to 'char []'
    'printPay' : cannot convert parameter 1 from 'char (*)[5][30]' to 'char []'
    I'm guessing the problem is with one of my pointers? I can't seem to figure out what exactly is wrong with my code though. Can anyone help?

    Code:
    #include <stdio.h>
    #include <string.h> 
    
    // gets name and hourlyrate and hours worked from user
    void userInput(char nameArray[], double hourlyRateArray[], double hoursWorkedArray[])
    	{
    		int i;
    		for (i=0; i<5; i++)
    		{
    		printf("Enter name.\n");
    		scanf("&#37;s", &nameArray[i]);
    		printf("What is their hourly rate?\n");
    		scanf("%f", &hourlyRateArray[i]);
    		printf("How many hours did they work this week?\n");
    		scanf("%f", &hoursWorkedArray[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[], double hoursWorkedArray[], double hourlyRateArray[])
    {
    	int i;
    	for (i=0; i<5; i++)
    	{
    		printf("Pay to %s: \n", nameArray[i]);
    		printf("Hours worked: %lf\n", hoursWorkedArray[i]);
    		printf("Hourly rate: $%.2lf\n", hourlyRateArray[i]);
    		printf(" Base pay: $%.2lf\n Gross pay: $%.2lf\n Gross pay: $%.2lf\n 
                    Net pay: $%.2lf\n Taxes paid: $%.2lf\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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > scanf("%s", &nameArray[i]);
    This should be:
    scanf("%s", nameArray[i]);

    scanf("%s", &nameArray[i]);
    printf("What is their hourly rate?\n");
    > scanf("%f", &hourlyRateArray[i]);
    To read a double use %lf (note: %f is fine in printf())
    scanf("%lf", &hourlyRateArray[i]);

    printf("How many hours did they work this week?\n");
    > scanf("%f", &hoursWorkedArray[i]);
    Same as above.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void userInput(char nameArray[], double hourlyRateArray[], double hoursWorkedArray[])
    The name array is two dimensional, so the declaration for nameArray should also be two dimensional:
    void userInput(char nameArray[][30], double hourlyRateArray[], double hoursWorkedArray[])
    Or you could also use:
    void userInput(char nameArray[5][30], double hourlyRateArray[], double hoursWorkedArray[])

    Ditto for printPay().

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please beware of scanf. It can be dangerous if not done right.
    Have a look at this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Code:
    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];
    			}
    		}
    	
    
    }
    you will get warning for above code...Function will not return more then one value at time..try to display in function only

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hai

    Code:
    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];
    			}
    		}
    	
    
    }
    you will get warning for above code...Function will not return more then one value at time..try to display in function only

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 02:29 PM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Passing by reference/using pointers
    By Wiretron in forum C++ Programming
    Replies: 11
    Last Post: 06-03-2007, 12:16 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM