Onionixed
Onionixed

Reputation: 63

Gets.chomp input comparison returning an error

I AM USING RUBY

ERROR: comparison of Integer with String failed (ArgumentError)

puts "Age: "
    age = gets.chomp
    if 0 < age < 130

I want the programm to allow the user to input all the numbers between 0 (not included) and 130 (included). How to do it?

Upvotes: 3

Views: 157

Answers (1)

Ursus
Ursus

Reputation: 30056

The input is a string. Try something like this

puts "Age: "
user_input = gets.chomp
begin
  age = Integer(user_input)
  # your code
rescue ArgumentError
  puts "Age must be an integer"
end

Upvotes: 3

Related Questions