A business offers a discount to certain customers. On some products, the discount is 10% of the gross price; on other products the discount is 15% of the net price.
There is a "discount" column on the invoices, in which the discount is printed by this code:
if discount_type.is_equal("gross") then
print("10%GROSS")
else
print("15%NET ")
end
Despite the use of a monospace font and the two extra spaces after "NET",
the invoice layout is messed up and the preprinted stationery is wasted
every time a customer purchases a product with a "net" discount. Why?
The problem arises because '%' is an escape character within Eiffel strings. The sequence "%N" is replaced by a newline, causing the rest of the page to be one line out of step.
So how do you print what you really want? This won't work:
print("15%")
print("NET ")
..because %" is the escape code for a double-quote within a string,
and therefore the string on the first line will never be terminated.
This will work:
print("15%%NET ")
..because %% is the escape code for a normal percent character.
If you want to print the discount onto two lines like this:
15% NET...you can do it in Eiffel like this:
print("15%%%NNET") -- no, it's not keyboard bounce!
[Aside: Early versions of Eiffel used the backslash as C does.]
Eiffel and NICE are registered trademarks of the Nonprofit International Consortium for Eiffel.