+ 3

Ruby Calculator using input?

How do you build a calculator in Ruby using an input and assuming the first character of the input is the integer and the second character is the operator and the third character is the second integer?

3rd Jan 2019, 7:43 AM
Ahmed Akhtar
Ahmed Akhtar - avatar
3 Réponses
+ 9
I just read this and niwo is right
5th Jan 2019, 4:47 AM
Jordan Kinney
Jordan Kinney - avatar
+ 2
You could use the .to_f method on a string to transform a string to a float(so floating point numbers will work to...). Use the .chomp method to remove the line break from the input, so you get the numbers. Your Program could start like: num1 = gets.chomp.to_f opr = gets.chomp num2 = gets.chomp.to_f After that, you switch between the operands you want to use. E.g.: if opr=="+" puts num1+num2 elsif opr=="-" ... ... ... end
3rd Jan 2019, 8:13 PM
Niwo
Niwo - avatar
+ 1
yes, like I have written a week ago...👍😂
10th Jan 2019, 6:06 PM
Niwo
Niwo - avatar