0

Need help with code

doesnt give an output what should i do about it operation=gets number1=gets number2=gets =begin Enter operation(/ * + - and 2 numbers in seperate lines) =end case operation when "+" puts number1+number2 when "-" puts number1-number2 when "/" puts number1/number when "*" puts number1*number2 end

22nd Nov 2016, 10:07 AM
Tolga
Tolga - avatar
4 Answers
+ 1
Hey dude! Here are some tips to maybe make it work. 1.CHANGE operation = gets INTO operation = gets.chomp 2. CHANGE number1 = gets INTO number1 = gets. to_f (do the same with your number2 variable) Explantions: 1. the chomp method will remove "\n" (newline) part of the operation input which results into matching your case criterias and of coursw running your case codes. 2. The default data type that we get from using gets is i believe a String so what happened in your num1 + num2 is that the 2 variables where treated as Strings not Numbers. Example you input 1 and 2, the result will be 12 which is wrong because 1 + 2 = 3. And the to_f method converts the input from String into a Fix number data type.
22nd Nov 2016, 11:11 PM
Hanz Cliebb Tura
Hanz Cliebb Tura - avatar
0
thanks man
22nd Nov 2016, 11:19 PM
Tolga
Tolga - avatar
0
Did it work?
22nd Nov 2016, 11:19 PM
Hanz Cliebb Tura
Hanz Cliebb Tura - avatar
0
Yup
22nd Nov 2016, 11:20 PM
Tolga
Tolga - avatar