-2

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 ;
  • 5
    Please paste code instead of linking images. – mavavilj Feb 21 '23 at 12:15
  • is this C or C++ ? Can you give the whole error messaage ? – nick Feb 21 '23 at 12:17
  • 4
    This is trivial to answer but please post text as text, not as images. Programmers work with text. – Lundin Feb 21 '23 at 12:20
  • Please **copy&paste** exactly the code you try to compile and run. You cannot add structures in C. (In C++ you might be able to define an operator `+`.) You have to add individual fields, e.g. `b1.bus+b2.bus+b3.bus`. – Bodo Feb 21 '23 at 12:57

2 Answers2

2

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

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.

mmixLinus
  • 1,646
  • 2
  • 11
  • 16