How do I fix Invalid Operands To Binary + (have 'struck _' and 'struct _ ')

84 Views Asked by At

It displays [Error] invalid operands to binary + (have 'struct bus' and 'struct bus') enter code here

struct bus
{int bus;
}b1,b2,b3;
int main()
{ 

printf("Enter bus fair 1");
scanf("%d", &b1.bus);

printf("Enter bus fair 2");
scanf("%d", &b2.bus);

printf("Enter bus fair 3");
scanf("%d", &b3.bus);

expenses = b1+b2+b3; 5*14*2 ;

printf("Travelling expense is; %d", expenses);

return 0;}

Did small changes, don't have much knowledge on this yet obviously.code image

Edit- It worked after changing the error line as this, but still I want to know why it didn't work in the previous code.

enter code here
    expenses = (b1.bus+b2.bus+b3.bus) ;5*14*2 ;
2

There are 2 best solutions below

1
mmixLinus On

You are trying to add + structs, instead of numbers. You should be referring to the actual integers within the structs for the binary operation + to work. (Binary means it is an operation that takes two operands ("input values").

I think you meant

expenses = b1.bus + b2.bus + b3.bus;

Note that in your original example you were adding b1 twice.

Also, if this is a comment, 5*14*2, write // before.

0
KamilCuk On

I want to know why it didn't work in the previous code.

Because b1 and b2 is a struct bus. Compiler has no idea how to compute struct bus + struct bus. Compiler can calculate only with basic types, like int + int.