2

Assume you are given three variables, revenue, expenses, and profit, all of type Money (a structured type with two int fields, dollars and cents). Assign to profit the result of subtracting expenses from revenue. Let's make the happy assumption that revenue exceeds expenses. However you still may find that the cents part of expenses exceeds that of revenue. If that is the case you will have to "borrow" 1 from revenue dollars (i.e. subtract 1) and "give" it to revenue's cents (i.e. add 100!) in order to carry out the subtraction properly.

Here is what I have but it's not working:

if (revenue.cents < expenses.cents)
   {
    revenue.dollars = revenue.dollars -1;
    revenue.cents = revenue.cents + 100;
    profit = revenue - expenses;
   }
else 
   {
    profit = revenue - expenses;
   }

I get this error message: error: no match for 'operator-' in 'revenue - expenses'

Any help is appreciated. Thanks.

Alti
  • 189
  • 1
  • 9
  • The compiler has no idea what you want to accomplish by subtracting two objects. You have to tell it. – chris Nov 18 '12 at 19:13

2 Answers2

3

You're getting that error because you cannot subtract one structure from another. You will have to define an operator-() function for your struct.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

You need to call each element of the structure and subtract them separately. MyProgLab will not allow you to define a function for this exercise. Not only that but if you enter the code that you have above it will tell you that 'you were not supposed to modify the element'. In order to avoid this you must conduct the borrowing of the dollar inside the arithmetic. Like this:

if(expenses.cents > revenue.cents) 
{
profit.dollars = (revenue.dollars - 1) - expenses.dollars;
profit.cents = (revenue.cents + 100) - expenses.cents;
}//end if
Justin
  • 1