hi
i just started programming so im very new to all this, i am writing a code and i am comparing a string in a struct to a string. one of them as you can tell is an array of strings, im not really sure if im breaking any rules by doing that.. but i get the error
"Warning: passing arg 1 of 'strcmp' makes pointer from integer without a cast"
i actually get 2 of those identically which point to the same line. can anyone help me out and show me what im doing wrong

for (j=0; j<numrecords; j++) {
      fscanf(fin, "%s %s", &firstname[j], &lastname[j]);
        for (a=0; a<numemp; a++) {
             
//THIS IS WHERE ITS MESSING UP
int b = strcmp(firstname[j], emp[a].last);
            int c = strcmp(lastname[j], emp[a].first);
            if (b == 0 && c == 0) 
                empid = a;
            

            else
                continue;
        }
      fscanf(fin, "%d%d%d%d", &hrin, &minin, &hrout, &minout);

      // Calculate the time worked and credit the appropriate employee.
      hrsworked[empid] += times(hrin, minin, hrout, minout);
        printf("%s\n", firstname[empid]);
    }

thanks
-Grant

Recommended Answers

All 11 Replies

Not without seeing how emp or firstname are declared. it would appear from the error that emp[a].last is of type int. Can you post more complete code?

yeah, i just didnt want to confuse anyone

// This program reads in a file with information about employees hourly
// salaries and working hours, and writes out to a file, a summary of
// their work and pay.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Set of constants used.
#define SIZE 20
#define MIN_IN_HR 60.0
#define REG_WEEK 40
#define OVERTIME 1.5
#define MAX_LEN 30

struct employee {
  char first[MAX_LEN];
  char last[MAX_LEN];
  double payperhr;
  double gross;
  double taxes;
  double hours_in_week;
};
double times(int hrin, int minin, int hrout, int minout);
double getPay(double hrs, double rate);
void init(double array[], int length, double val);

int main() {

  // hrsworked stores the number of hours each employee works in a week.
  // totalhrs stores the total number of hours each employee has worked.
  // payrate stores each employee's hourly payrate.
  // totalpay stores each employee's total pay.
  struct employee emp[SIZE];
  double hrsworked[SIZE], totalhrs[SIZE], payrate[SIZE], totalpay[SIZE];
  double pay;
  char firstname[MAX_LEN], lastname[MAX_LEN];
  int numemp, numrecords, empid, total_weeks;
  int hrin, minin, hrout, minout;
  int i, j, a;

  FILE *fin;
  FILE *fout;

  // Open both the input and output files.
  fin = fopen("clock2.txt", "r");
  fout = fopen("w2.txt", "w");

  // Initialize each necessary array to 0.
  init(totalhrs, SIZE, 0);
  init(totalpay, SIZE, 0);

  // Read in and print out the number of employees.
  fscanf(fin, "%d", &numemp);
  fprintf(fout, "Number of employees: %d\n", numemp);

  // Read in each employee's hourly pay.
  for (i=0; i<numemp; i++) {
     fscanf(fin, "%s %s", &(emp[i].first), &(emp[i].last));
     fscanf(fin, "%lf", &(emp[i].payperhr));
  }
    fscanf(fin, "%d", &total_weeks);
  // Read through each week's clocking in-out data.
  for (i=1; i<=total_weeks; i++) {

    // Read in the number of records for the week.
    fscanf(fin, "%d", &numrecords);
    init(hrsworked, SIZE, 0);

    // Read in each piece of data, and add the appropriate hours for the
    // appropriate employee.
    for (j=0; j<numrecords; j++) {
      fscanf(fin, "%s %s", &firstname[j], &lastname[j]);
        for (a=0; a<numemp; a++) {
    //    printf("%s %s\n", firstname[j], lastname[j]);
            int b = strcmp(firstname[j], emp[a].last);
            int c = strcmp(lastname[j], emp[a].first);
            if (b == 0 && c == 0) //strcmp(firstname[j], emp[a].last) == 0) // && strcmp(lastname[j], emp[a].first)==0)
                empid = a;
            
            //    printf("yo\n");
            else
                continue;
        }
      fscanf(fin, "%d%d%d%d", &hrin, &minin, &hrout, &minout);

      // Calculate the time worked and credit the appropriate employee.
      hrsworked[empid] += times(hrin, minin, hrout, minout);
        //printf("%d\n", empid);
    //    printf("%s\n", emp[empid].last);
        printf("%s\n", firstname[empid]);
    }

    // Print out the necessary information for the week for each employee.
    for (j=0; j<numemp; j++) {

      // Calculate the pay for the week for employee j, and adjust his/her
      // total pay and hours.
      pay = getPay(hrsworked[j], emp[j].payperhr);
      totalpay[j] += pay;
      totalhrs[j] += hrsworked[j];
    }
    
  }

  // Print out the final chart header.


  // Print out all the totaled information.
  for (i=0; i<numemp; i++) {
    fprintf(fout, "\nW2 Form\n");
     fprintf(fout, "-------\n");
     fprintf(fout, "Name: %s %s\n", emp[i].first, emp[i].last);
     fprintf(fout, "Gross Pay: \n");
     fprintf(fout, "Taxes Withheld: \n");
     fprintf(fout, "Net Pay: \n");  
     fprintf(fout, "%d\t%.2lf\t%.2lf\n", i, totalhrs[i], totalpay[i]);
  }
  // Close both the input and output files.
  fclose(fin);
  fclose(fout);

  return 0;
}

// Returns the time in hours in between the time specified by the first
// two parameters and the time specified by the last two parameters.
double times(int hrin, int minin, int hrout, int minout) {
  return (hrout-hrin) + (minout-minin)/MIN_IN_HR;
}

// Returns the pay for an employee who works hrs number of hours in a week
// at an hourly pay of rate.
double getPay(double hrs, double rate) {
  if (hrs <= REG_WEEK)
    return hrs*rate;
  else
    return REG_WEEK*rate + (hrs-REG_WEEK)*OVERTIME*rate;
}

// Sets indexes 0 through length-1 in array to val.
void init(double array[], int length, double val) {

  int i;
  for (i=0; i<length; i++)
    array[i] = val;
}

and i attached the input file and sample output file, ive been looking through it all day, and my eyes are gonna fall out if i check it anymore.

Ah, OK - the problem is actually your firstname variable - which is an array of char.

firstname[j] is a single character - the problem is that strcmp is expecting a whole string, ie char* or const char*

did you mean firstname to be an array of strings? or do you intend it to be a single string? if it's a single string, get rid of the [j] at the end

for an array of strings, you need to declare firstname as char* firstname[]

ok, i get it, um.. i dont know how to work the whole pointer for array of strings so i just did this

char firstname[SIZE][MAX_LEN], lastname[SIZE][MAX_LEN];

it seems to be working,but how exactly would i use the pointer instead(char* firstname[] etc)? By the way, thanks so much for the help, this program has been driving me nuts:D

ok, i get it, um.. i dont know how to work the whole pointer for array of strings so i just did this

char firstname[SIZE][MAX_LEN], lastname[SIZE][MAX_LEN];

it seems to be working,but how exactly would i use the pointer instead(char* firstname[] etc)? By the way, thanks so much for the help, this program has been driving me nuts:D

The two forms are almost equivalents (With the one caveat that your form explicitly specifies the size of both array 'dimensions') - although the explanation as to why is not quite so trivial ;)

Essentially, a 'C' String is a series of characters linked in contiguous memory (Plus a null terminating character - that's the '\n' thing).. or, as you know it, an array of char.

However, a variable which holds a 'C' String is not an array but a pointer to the first element of that array.

char* myName = "Fred";
char myNickname[] = "Bloggsy";
        // These are equivalent.

With this in mind, a 2D array of characters is not really 2-Dimensional. it is a single array - each element containing a pointer.

So, you have your variable which "contains"(sic) the 2D array (the array of pointers) - but that variable doesn't actually contain an array, it points to the first element.

Hence, the following declarations are equivalent:

char* name[];
char** name;
char name[][];

in every case, the data-type of name is pointer-to-pointer (to char)


Confused? ;)

thanks so much for the help. im pretty sure i understand it now.
-Grant

However, a variable which holds a 'C' String is not an array but a pointer to the first element of that array.

char* myName = "Fred";
char myNickname[] = "Bloggsy";
        // These are equivalent.

No, they are not equivalent. Similar, but not the same.

With this in mind, a 2D array of characters is not really 2-Dimensional. it is a single array - each element containing a pointer.

This is a bit of misinformation.

So, you have your variable which "contains"(sic) the 2D array (the array of pointers) - but that variable doesn't actually contain an array, it points to the first element.

Hence, the following declarations are equivalent:

char* name[];
char** name;
char name[][];

in every case, the data-type of name is pointer-to-pointer (to char)


Confused? ;)

Yes. Because most of that wasn't correct.

I didn't say they were identical, I said they were equivalents :)

From a data point of view, these are the same

char* x = "hello";
char x[] = "hello";

In the fact that both create an array of char, size 6, and a pointer.

the difference is that the char* x version leaves the pointer variable 'x' non-const (so may be reassigned later), whilst the char x[] version creates 'x' as a const pointer object.

I didn't say they were identical, I said they were equivalents :)

From a data point of view, these are the same

char* x = "hello";
char x[] = "hello";

In the fact that both create an array of char, size 6, and a pointer.

the difference is that the char* x version leaves the pointer variable 'x' non-const (so may be reassigned later), whilst the char x[] version creates 'x' as a const pointer object.

So after following the links posted by Salem, do you now know that both do NOT "create an array of char, size 6, and a pointer"?

I've read the C FAQ before, admittedly not all the way through, although I can see that I misinterpreted some of the explanation of the subscript operator 'decaying' into a pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.