Zeptuz
Zeptuz

Reputation: 103

How to divide a character with a string in Ada

I'm trying to figure out how to divide a character with a string and get a float as the quota. I've done the following in my code:

 Procedure extwo is

 function  "-"(Strang: in String;
               Char: in Character) return Float is
  
   F: Float := Float'Value(Strang);
  
 begin
        
   return Float(Character'Pos(Char)-48) - Float'Value(Strang);
 
 end "-"; 

 Differensen: Float;
 Char: Character;
 Strang: String(1.
                
Begin 

 Put("Mata in ett tecken: ");
 Get(Char);
  
 Put("Mata in en sträng med exakt 3 tecken: ");
 Get(Strang);

 Differensen:= Char-Strang;

 Put("Du matade in tecknet: ");
 Put(Char);
 Put(" och strängen: ");
 Put(Strang);
 Put(" och differensen blev ");
 Put(Differensen, Fore=> 0); 

end extwo;

With this I get the error messages: " invalid operand types for operator "-" ", " left operand has type "Standard.Character" " and " right operand has subtype of "Standard.String" defined at line 59 " all on line 95:22 which is the line where it says "Differensen:= Char-Strang;"

Upvotes: 0

Views: 119

Answers (1)

Simon Wright
Simon Wright

Reputation: 25501

It’s easier with operators to use the names L for the parameter that goes on the left of the operator, R for the parameter that goes on the right.

Which, in order to match your usage (Char - Strang) and your implementation would be

function "-" (L : Character; R : String) return Float;

Upvotes: 2

Related Questions