Having problem comparing two predefined values in control flow


#1

Here is the code which i wanted to execute but continously getting errors. Any help would be appreciated.

puts "Number please:"
num = gets.chomp
mun = 100
if num < mun
 puts "#{num}"
else
  puts "#{mun}"
end

#2

What happens when you run the code? What do you expect to happen?


#3

Following is returned when I try to run this code.
F:\Ruby\Ruby24-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb
Number please:
67
C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb:4:in <': comparison of String with 100 failed (ArgumentError) from C:/Users/Vishu/.RubyMine2017.2/config/scratches/scratch.rb:4:in<top (required)>'
from -e:1:in load' from -e:1:in

Process finished with exit code 1

But my expectation was that it should return the variable num if I enter a number smaller than 100.


#4

num (the user input) is of type string, mun is of type integer

comparing this two different data types, bad idea. Maybe convert the user input to integer?


#5

Yesterday I had the idea to make a program in ruby that can provide the mathematical tables for children using user input i.e…any number . So I wanted to do so using combination of control flow and loops. Can you help me building such programs. Any help would be appreciated.


#6

That is a different question entirely. I haven’t programmed in ruby in ages, but you need to convert user input to integer:

num = gets.chomp
num = num.to_i

then a simple for loop:

for i in 0..10:
    puts i * num

to display the multiplication table.


#7

Thanks for Help. Contact You soon in case of any query.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.