Invalid Operands to Binary + Error

Having trouble figuring out how to fix this error

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3331 Views - Last Post: 03 December 2010 - 07:34 PM Rate Topic: -----

#1 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Invalid Operands to Binary + Error

Posted 03 December 2010 - 11:20 AM

#include <stdio.h>

#define SIZE 20 
#define MAX_LEN 30

struct employee {
  char first[MAX_LEN];
  char last[MAX_LEN];
  double payperhr;
  double gross;
  double taxes;
  double hours_in_week;
};

  

int main() {

int i, j, n, k, m, hrin, minin, hrout, minout, temphours;
char first, last, L;

FILE* ifp;
ifp = fopen("clock2.txt","r");

struct employee workers[SIZE];

for (i=0; i<n; i++) {
fscanf (ifp,"%s%s%lf", workers [i].first, workers [i].last, workers [i].payperhr);
}

fscanf(ifp,"%d", &k);

for(i=0; i<k; i++) {
    for(j=0; j<n; j++) {
        workers [j].hours_in_week = 0;
        }
        
        fscanf(ifp, "%d", &m);
    
    for(i=0; i<m; i++) {
       fscanf(ifp, "%s%s%d%d%d%d", last, first, &hrin, &minin, &hrout, &minout);
       temphours = calc_hours(hrin, minin, hrout, minout);
       
       for(L=0; L<n; L++) {
          if(strtcmp(first, workers[L].first) == 0 && (last, workers[L].last) == 0) {
             workers[L] += temphours; //line where I receive the error
             }
             }
}             
                              
     for(j=0; j<n; j++) {
         if(workers[j].hours_in_week > 40) {
            workers [j].gross += (40 * workers[j].payperhr) + (workers[j].hours_in_week        
            -40) * 1.5 * workers[j].payperhr;
            
            workers[j].taxes += 0.1 * (40 * workers[j].payperhr) + 0.2 * 
            (workers[j].hours_in_week - 40) * 1.5 * workers[j].payperhr;
            
            }
            }
}            
fclose(ifp);

FILE *ofp;
ofp = fopen("w2.txt", "w");

fprintf(ofp, "w2 Form\n");
fprintf(ofp, "-------\n");

for(j=0; j<n; j++)
    fprintf(ofp, "Name %s  %s\n", workers[j].first, workers[j].last);
    fprintf(ofp, "Gross Pay %lf\n", workers[j].gross);
    fprintf(ofp, "Taxes Withheld %lf\n", workers[j].taxes);
    fprintf(ofp, "Net Pay %lf", workers[j].gross - workers[j].taxes);
    

system("PAUSE");
return 0;
}

double calchours(int hrin, int minin, int hrout, int minout) {
       int hours = hrout - hrin;
       int min = minout - minin;
       if(min < 0) {
          min += 60;
          hours--;

}
double tothours = hours + 1.0 * min/60;
return tothours; 
}          



I apologize for my stying if it's difficult to read. Line 46 is where I receive the error. Thanks in advance for any help :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: Invalid Operands to Binary + Error

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 11:33 AM

The compiler doesn't know what to invoke in order to add to a 'type' of 'employee'. You need to add code to do this.
Was This Post Helpful? 2
  • +
  • -

#3 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 11:38 AM

Thanks for the response. Would you mind explaining this? I'm relatively new to programming and not really sure what code to add.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 11:47 AM

workers[L] is a worker, not a number.
temphours is an integer, not a worker.

Now can you see that you can't simply "add" these two things together?

Presumably you wanted to add the number of temphours to the hours_in_week field that belongs to that employee, but you have to tell that to the compiler explicitly.
Was This Post Helpful? 2
  • +
  • -

#5 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 11:53 AM

Ahhh I see. Wow that was so obvious. I really appreciate the help :smile2:
Was This Post Helpful? 0
  • +
  • -

#6 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:09 PM

I have one more question: When I try to compile I get linker errors. These include undefined reference to 'calchours' and undefined reference to 'strtcmp'. I'm not sure what this means.
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:12 PM

Can you copy any paste the exact messages, please? :)
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:15 PM

It's not strtcmp, it's strcmp. And you should #include <string.h> if you're using strcmp.
Was This Post Helpful? 1
  • +
  • -

#9 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:16 PM

Of course:

[Linker error] undefined reference to `calc_hours'
[Linker error] undefined reference to `strtcmp'
ld returned 1 exit status
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:17 PM

View PostJackOfAllTrades, on 03 December 2010 - 06:15 PM, said:

It's not strtcmp, it's strcmp. And you should #include <string.h> if you're using strcmp.

Oh yeah! :)
Was This Post Helpful? 0
  • +
  • -

#11 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:20 PM

Thanks JackOfAllTrades. That got rid of the linker error but now I get this error message:

45 [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast.
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 12:22 PM

You've called

double calchours(int hrin, int minin, int hrout, int minout)


Before declaring it.

Put this at the top of your code after your struct definition.

double calchours(int hrin, int minin, int hrout, int minout);



:)
Was This Post Helpful? 1
  • +
  • -

#13 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 05:10 PM

I'm not sure if this thread is still active but I'm still getting one error in my code:

"[Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast"

This shows up for line 45 of my code. I just can't seem to fix it :dontgetit:

This post has been edited by HeAdHuNtErKg: 03 December 2010 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 05:16 PM

You're supposed to be comparing two strings. On line 45, your arg 1 (the first argument you are passing to strcmp) is first, which is a char, not a char array.

Also, I just noticed that you also declared last to be a char, rather than an array. You need to change both of those to arrays big enough to accomodate the strings that you're storing in them.
Was This Post Helpful? 1
  • +
  • -

#15 HeAdHuNtErKg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 10

Re: Invalid Operands to Binary + Error

Posted 03 December 2010 - 05:41 PM

Do I declare these arrays in the struct employee definition before main or in the 'strcmp' itself? Sorry I'm just trying to grasp this stuff.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2