0

I have a problem with the if statment.

I have a program with commandline-arguments and utf8 (€ - Symbol).

The error is in works_not in the if statement.

class EURO

insert ARGUMENTS

create {ANY}
   make

feature {ANY}

   make
      do
         works_not
         works
      end

   works_not
      local ok: BOOLEAN
      do
         print ("%N%NAnzahl Argumente          : " + argument_count.to_string + "%N")
         print ("%NArgument -> Programmname    : " + argument(0))
         print ("%NArgument -> Wert            : " + argument(1))
         print ("%NArgument -> Währung         : " + argument(2) + "%N")

         ok := argument(2) = "€" 
         print ("%NArgument(2) ist Euro ?  " + ok.to_string + "%N%N")

         print ("don't work")
         io.put_new_line

         if argument(2) = "€" then
            euro_in_dm(argument(1).to_real)
         else
            dm_in_euro(argument(1).to_real)
         end
      end

   works
      do
         print ("works ")
         io.put_new_line

         if argument_count /= 2 then
            print("%N%N Error (1) %N%N")    
         else
            inspect 
               argument(2) 
            when "€" then
               euro_in_dm(argument(1).to_real)
            when "DM","dm" then
               dm_in_euro(argument(1).to_real)
            else
               print("%N%N Error (2) %N%N")
            end
         end
      end

feature

   euro_in_dm (a: REAL)
      do
         io.put_string("%N Euro -> DM ")
         io.put_real(a * 1.95583)
         io.put_string("%N%N")
      end

   dm_in_euro (a: REAL)
      do
         io.put_string("%N DM -> Euro ")
         io.put_real(a / 1.95583)
         io.put_string("%N%N")
      end

end
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35

1 Answers1

1

The issue is in the comparison operator argument(2) = "€".

In Eiffel strings have a reference type, so the equality operator = compares references to the string objects, not their contents. If you want to compare the string values instead, you need to use an operator ~ that internally calls is_equal after checking that types of both operands are exactly the same, or a more robust version same_string (provided that it is available in your version of the environment). To summarize, you can try one of the following instead of the equality:

  • argument(2).same_string ("€")
  • argument(2) ~ "€"
  • argument(2).is_equal ("€")
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • i have write the program for any years. I have change the code to "is_equal" and with liberty eiffel all is ok. But now i have install ise-eiffel and nothing works . I have change "instert" to "inherit". 2 errors first the inspect statment (don't work with string ?) second the "€" Symbol (UTF-8) don't work -- one argument not two. sorry for my english. – user7450201 Jun 01 '19 at 09:54
  • For Unicode arguments you need to use `ARGUMENTS_32` instead of arguments. Then, `"€"` should be written as `{STRING_32} "€"`. And `inspect` does not work on strings in EiffelStudio, that's why the suggestion is to use `if` with `same_string`. – Alexander Kogtenkov Jun 29 '19 at 15:29