Thread: To understand and figure out why char *ARRAY cannot convert to char* for function

  1. #1
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108

    To understand and figure out why char *ARRAY cannot convert to char* for function

    My code is provided at the bottom. These are the error messages I receive when compiling:
    Code:
    g++ assign2c.cpp taxes.o -o main.exe
    assign2c.cpp: In function ‘int main()’:
    assign2c.cpp:72:65: error: cannot convert ‘char (*)[5][12]’ to ‘char*’ for argument ‘1’ to ‘void inputEmpData(char*, char*, float*, float*, float*)’
    make: *** [main.exe] Error 1
    What I want to do is put this small section of code into a function but it says I can't convert char[ARRAY] to char* for argument 1 to void 'inputEmpData' this is where information about the employee is entered.
    Code:
    printf("Enter the last name of the employee\n");
      scanf("%s", lastname);
      printf("Enter the first name of the employee\n");
      scanf("%s", firstname);
      printf("Enter the the pay of the employee\n");
      scanf("%f", payrate);
      printf("Enter the amount of hours worked\n");
      scanf("%f", hrs);
      printf("Enter the deferred amount\n");
      scanf("%f", deferred);
    The function goes in the body of the for loop as so:
    Code:
    for(i=0; i<ARRAYSIZE; i++)
      {
       inputEmpData(&lastname, &firstname, &payrate, &hrs, &deferred); //here goes function of inputEmpData
       printf(BLANKLINES);
      }
    Code:
    /*   Program: Assign2c
         Course: CISP360
         Author: Arturo
         Purpose: Payroll Program
         Date: 03/20/13
    
         Software   Change    Record
         Date       Who       What
         03/20      Arturo    Second Program
    */
    
    /*      Decomposition
       3.0 Payroll
    	3.1 PrintReportHeadings
    	3.2 InitializeAccumulators
    	3.3 InputEmployeeData
    	3.4 CalculateGross
    	3.5 CalculateTaxes
    		3.5.1 CalcFedtax
    		3.5.2 CalcStatetax
    		3.5.3 CalcSSItax
    	3.6 AddDetailToAccumulator
    	3.7 PrintSummaryReport
    */
    extern void calculatetaxes(float gross,float deferred, float *ft, float *st, float ........i);
    void ovtHrs(float *hrs_wrk, float *ovt_hrs, float hrs, float *gross, float payrate);
    void netPay(float gross, float deferred, float ft, float st, float ssi, float *net);
    void calcAccumulator(float *tot_payrate, float payrate, float *tot_hrs_wrk, float hrs_wrk, float *tot_ovt, float ovt_hrs, 
                         float *tot_gross, float gross, float *tot_ft, float ft, float *tot_st, float st, float *tot_ssi, float ssi,
                         float *tot_deferred, float deferred,  float *tot_net, float net);
    void calcAverage(float tot_payrate, float *payrate_avg, float tot_hrs_wrk, float *hrs_wrk_avg, float tot_ovt, float *ovt_avg, 
                     float tot_gross, float *gross_avg, float tot_ft, float *ft_avg, float tot_st, float *st_avg, float tot_ssi, 
                     float ........i_avg, float tot_deferred, float *deferred_avg, float tot_net, float *net_avg);
    void initializeAccumulators(float *tot_payrate, float * tot_hrs_wrk, float  *tot_ovt, float  *tot_gross, float  *tot_ft, 
                                float  *tot_st, float  *tot_ssi, float  *tot_deferred, float  *tot_net);
    void inputEmpData(char *lastname, char *firstname, float *payrate, float *hrs, float *deferred);
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define BLANKLINES    "                                                                                                \n\n"
    #define REPORTHEADER  "     Employee                      Pay            Reg Hrs          Gross              Fed            SSI        Net\n"
    #define REPORTHEADER1 "     Name                          Rate           Ovt Hrs          Pay                State          Defr       Pay\n"
    #define REPORTHEADER2 "     =========                    ======         =========        =======            =======        ======     =====\n"
    #define REPORTFORMAT  "%18s           %10.2f %15.2f %17.2f %18.2f%15.2f%11.2f\n"
    #define REPORTFORMAT1 "                    %35.2f %36.2f   %12.2f\n"
    #define REPORTFORMAT2 "     Totals%28.2f%16.2f%18.2f%19.2f%15.2f%11.2f\n"
    #define REPORTFORMAT3 "     %50.2f%37.2f%15.2f\n"
    #define REPORTFORMAT4 "     Averages%26.2f%16.2f%18.2f%19.2f%15.2f%11.2f\n"
    #define REPORTFORMAT5 "      %49.2f%37.2f%15.2f\n"
    #define ARRAYSIZE 5
    
    
    
    int main(void)
    {
      char lastname[ARRAYSIZE][12], firstname[ARRAYSIZE][10];
      char S_name[12+10];
      float payrate[ARRAYSIZE], hrs_wrk[ARRAYSIZE], ovt_hrs[ARRAYSIZE], gross[ARRAYSIZE], ft[ARRAYSIZE], st[ARRAYSIZE], ssi[ARRAYSIZE], deferred[ARRAYSIZE], net[ARRAYSIZE], hrs[ARRAYSIZE];
      float tot_payrate, tot_hrs_wrk, tot_ovt, tot_gross, tot_ft, tot_st, tot_ssi, tot_deferred, tot_net;
      float payrate_avg, hrs_wrk_avg, ovt_avg, gross_avg, ft_avg, st_avg, ssi_avg, deferred_avg, net_avg;
      int i;
      FILE * reportFILE;
      reportFILE=fopen("./report.txt","wt");
      if (reportFILE==NULL)
      {
        printf("Report File open failed...\n");
        exit(-80); //terminate app-reqs <stdlib.h>
      }
      for(i=0; i<ARRAYSIZE; i++)
      {
       inputEmpData(&lastname, &firstname, &payrate, &hrs, &deferred); //here goes function of inputEmpData
       printf(BLANKLINES);
      } 
    
      printf(BLANKLINES);
      fprintf(reportFILE, REPORTHEADER);
      fprintf(reportFILE, REPORTHEADER1);
      fprintf(reportFILE, REPORTHEADER2);
    
      initializeAccumulators(&tot_payrate, &tot_hrs_wrk, &tot_ovt, &tot_gross, &tot_ft, &tot_st, &tot_ssi, &tot_deferred, &tot_net);
    
      for(i=0; i<ARRAYSIZE; i++)
      {
        strcpy(S_name, lastname[i]);
        strcat(S_name, ", ");
        strcat(S_name, firstname[i]); 
    
        ovtHrs(&hrs_wrk[i], &ovt_hrs[i], hrs[i], &gross[i], payrate[i]);
        calculatetaxes(gross[i], deferred[i], &ft[i], &st[i], &ssi[i]);
        netPay(gross[i], deferred[i], ft[i], st[i], ssi[i], &net[i]);
        calcAccumulator(&tot_payrate, payrate[i], &tot_hrs_wrk, hrs_wrk[i], &tot_ovt, ovt_hrs[i], &tot_gross, gross[i], &tot_ft, ft[i], &tot_st, st[i], &tot_ssi, ssi[i], &tot_deferred, deferred[i], &tot_net, net[i]);
        calcAverage(tot_payrate, &payrate_avg, tot_hrs_wrk, &hrs_wrk_avg, tot_ovt, &ovt_avg, tot_gross, &gross_avg, tot_ft, &ft_avg, tot_st, &st_avg, tot_ssi, &ssi_avg, tot_deferred, &deferred_avg, tot_net, &net_avg);
    
        printf(BLANKLINES);
        fprintf(reportFILE, REPORTFORMAT, S_name, payrate[i], hrs_wrk[i], gross[i], ft[i],  ssi[i], net[i]);
        fprintf(reportFILE, REPORTFORMAT1, ovt_hrs[i],st[i], deferred[i]);
      }
      printf(BLANKLINES);
      fprintf(reportFILE, REPORTFORMAT2, tot_payrate, tot_hrs_wrk, tot_gross, tot_ft, tot_ssi, tot_net);
      fprintf(reportFILE, REPORTFORMAT3, tot_ovt, tot_st, tot_deferred);
      fprintf(reportFILE, REPORTFORMAT4, payrate_avg, hrs_wrk_avg, gross_avg, ft_avg, ssi_avg, net_avg);
      fprintf(reportFILE, REPORTFORMAT5, ovt_avg, st_avg,deferred_avg);
      fclose(reportFILE);
    
      while(getchar()!= '\n');
      getchar;
      return 0;
    }
    
    void inputEmpData(char *lastname, char *firstname, float *payrate, float *hrs, float *deferred) 
    {
      printf("Enter the last name of the employee\n");
      scanf("%s", lastname);
      printf("Enter the first name of the employee\n");
      scanf("%s", firstname);
      printf("Enter the the pay of the employee\n");
      scanf("%f", payrate);
      printf("Enter the amount of hours worked\n");
      scanf("%f", hrs);
      printf("Enter the deferred amount\n");
      scanf("%f", deferred);
    }
    
    
    void ovtHrs(float *hrs_wrk, float *ovt_hrs, float hrs, float *gross, float payrate)
    {
     if(hrs<=40)
     {
       *ovt_hrs=0;
       *hrs_wrk=hrs;
       *gross=hrs*payrate;
     }
     else
     {
       *hrs_wrk=40;
       *ovt_hrs=hrs-40;
       *gross=payrate*40+(hrs-40)*1.5*payrate;
     }
    }
    
    void netPay(float gross, float deferred, float ft, float st, float ssi, float *net)
    {
     *net=gross-deferred-ft-st-ssi;
    }
    
    void initializeAccumulators(float *tot_payrate, float * tot_hrs_wrk, float  *tot_ovt, float  *tot_gross, float  *tot_ft, float  *tot_st, float  *tot_ssi, float  *tot_deferred, float  *tot_net)
    {
      *tot_payrate=0;
      *tot_hrs_wrk=0;
      *tot_ovt=0;
      *tot_gross=0;
      *tot_ft=0;
      *tot_st=0;
      *tot_ssi=0;
      *tot_deferred=0;
      *tot_net=0;
    }
    
    void calcAccumulator(float *tot_payrate, float payrate, float *tot_hrs_wrk, float hrs_wrk, float *tot_ovt, float ovt_hrs, float *tot_gross, float gross, float *tot_ft, float ft, float *tot_st, float st, float *tot_ssi, float ssi,float *tot_deferred, float deferred,  float *tot_net, float net)
    {
     *tot_payrate+=payrate;
     *tot_hrs_wrk+=hrs_wrk;
     *tot_ovt+=ovt_hrs;
     *tot_gross+=gross;
     *tot_ft+=ft;
     *tot_st+=st; 
     *tot_ssi+=ssi;
     *tot_deferred+=deferred;
     *tot_net+=net;
    }
    
    
    void calcAverage(float tot_payrate, float *payrate_avg, float tot_hrs_wrk, float *hrs_wrk_avg, float tot_ovt, float *ovt_avg, float tot_gross, float *gross_avg, float tot_ft, float *ft_avg, float tot_st, float *st_avg, float tot_ssi, float ........i_avg, float tot_deferred, float *deferred_avg, float tot_net, float *net_avg)
    {
     *payrate_avg=(tot_payrate/5);
     *hrs_wrk_avg=(tot_hrs_wrk/5);
     *ovt_avg=(tot_ovt/5);
     *gross_avg=(tot_gross/5);
     *ft_avg=(tot_ft/5);
     *st_avg=(tot_st/5);
     ........i_avg=(tot_ssi/5);
     *deferred_avg=(tot_deferred/5);
     *net_avg=(tot_net/5);
    }
    Last edited by arti; 04-09-2013 at 05:25 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    because a two dimension char array is not a char array. it is an array of char arrays. neither are the numeric parameters floats, they arrays of floats. for each of those arguments you need to pass a pointer to the particular element of the array subscripted by your loop index, i
    note that the float arrays need an ampersand because they are individual elements, where the two char arrays degenerate to char *'s so you don't want the ampersand on them
    Code:
    inputEmpData(lastname[i], firstname[i], &payrate[i], &hrs[i], &deferred[i]); //here goes function of inputEmpData

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Because your function expects a pointer to a character, which can also be a one dimentional array. The argument you are trying to pass to it is a two dimentional array.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by dmh2000 View Post
    because a two dimension char array is not a char array. it is an array of char arrays. neither are the numeric parameters floats, they arrays of floats. for each of those arguments you need to pass a pointer to the particular element of the array subscripted by your loop index, i
    note that the float arrays need an ampersand because they are individual elements, where the two char arrays degenerate to char *'s so you don't want the ampersand on them
    Code:
    inputEmpData(lastname[i], firstname[i], &payrate[i], &hrs[i], &deferred[i]); //here goes function of inputEmpData
    Would the prototype and the definition have "[i]"? I'm sorry it's the first time I'm working with pointers and functions like this.

  5. #5
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Code:
    g++ assign2c.cpp taxes.o -o main.exe
    assign2c.cpp:36:33: error: ‘i’ was not declared in this scope
    assign2c.cpp:36:35: error: expected ‘)’ before ‘,’ token
    assign2c.cpp:36:37: error: expected unqualified-id before ‘char’
    assign2c.cpp:112:33: error: ‘i’ was not declared in this scope
    assign2c.cpp:112:35: error: expected ‘)’ before ‘,’ token
    assign2c.cpp:112:37: error: expected unqualified-id before ‘char’
    make: *** [main.exe] Error 1
    I get these errors and still do even though I have declared 'i' as an integer in the function definition. But I shouldn't have to do this, right? The i is already being declared in the main of the code, the for loop is in the main and the function call is in the for loop. This is what the prototype, the call and the function definition look like. What am I doing wrong?

    Code:
    void inputEmpData(char lastname[i], char firstname[i], float *payrate[i], float *hrs[i], float *deferred[i]);
    Code:
    for(i=0; i<ARRAYSIZE; i++)
      {
       inputEmpData(lastname[i], firstname[i], &payrate[i], &hrs[i], &deferred[i]); //here goes function of inputEmpData
       printf(BLANKLINES);
      }
    Code:
    void inputEmpData(char lastname[i], char firstname[i], float *payrate[i], float *hrs[i], float *deferred[i])
    {
      int i;
      printf("Enter the last name of the employee\n");
      scanf("%s", lastname[i]);
      printf("Enter the first name of the employee\n");
      scanf("%s", firstname[i]);
      printf("Enter the the pay of the employee\n");
      scanf("%f", &payrate[i]);
      printf("Enter the amount of hours worked\n");
      scanf("%f", &hrs[i]);
      printf("Enter the deferred amount\n");
      scanf("%f", &deferred[i]);
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why did this change from
    Code:
    void inputEmpData(char *lastname, char *firstname, float *payrate, float *hrs, float *deferred);
    To what is almost positively wrong
    Code:
    void inputEmpData(char lastname[i], char firstname[i], float *payrate[i], float *hrs[i], float *deferred[i]);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by stahta01 View Post
    Why did this change from
    Code:
    void inputEmpData(char *lastname, char *firstname, float *payrate, float *hrs, float *deferred);
    To what is almost positively wrong
    Code:
    void inputEmpData(char lastname[i], char firstname[i], float *payrate[i], float *hrs[i], float *deferred[i]);
    I'm still learning. I haven't worked with functions and arrays like this. I thought just like the star * that is typed in the function prototype and the function definition so would the "I" be typed the same way, in the definition and prototype.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Why not make a struct that defines one element of your data, with each struct storing data about exactly one individual? Then you can make one array of structs, rather than n arrays of primitives. So much easier, too, in terms of passing the data around and manipulating it.

    If you ever have a case where you have multiple arrays, where the ith element of each array is related to the ith element of the other arrays, chances are you're doing something wrong.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    Quote Originally Posted by Cat View Post
    Why not make a struct that defines one element of your data, with each struct storing data about exactly one individual? Then you can make one array of structs, rather than n arrays of primitives. So much easier, too, in terms of passing the data around and manipulating it.

    If you ever have a case where you have multiple arrays, where the ith element of each array is related to the ith element of the other arrays, chances are you're doing something wrong.
    Making a struct is in the plans. That's later on in the course that I am taking. For the moment I'm trying to make that into a function. Also I been reading up on structs and I can't make heads or tails about them

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. Cannot understand 'char' array text problem.
    By xheavenlyx in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2010, 04:28 AM
  3. Convert char array to int
    By McFry in forum C Programming
    Replies: 9
    Last Post: 07-12-2007, 12:40 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. help on a function of INT > CHAR convert!
    By SublicK in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 01:21 PM