0

First, I want to say that I am new to ruby, and I am still learning. I am currently working on a school project where the assignment is to make a program calculate the user's input.

However, in my program I want it to only permit the user to type in numbers. If the user gives any form for letters it will get an error, and make the user try again until he gives a number. Any way of doing this?

Thank you so much, if you can help me:)

Here is my code so far:

def calculate_one #method to calculate option 1

  prompt = "> " #make it look nice
  puts "What is the salary for the accountant? " 
  print prompt 
  while @accountant = gets.chomp #looping 

    if @accountant =~ /\A\d+\z/

      puts "Error: Only use numbers. Please try again"
      print prompt 

    else
      "What is the salary for the developer? "
      print prompt

      while @developer = gets.chomp

        if @developer =~ /\A\d+\z/
          puts "Error: Only use numbers. Please try again"
          print prompt

        else 
          print "The salary for an account is #{@accountant}\nThe salary for an developer is #{@developer}"

          break
        end
      end 
    end
  end
end
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
  • 1
    I've fixed the indentation, but you should always do this yourself – max pleaner Nov 22 '17 at 16:25
  • I'm voting to close this question as off-topic because you have already asked this question here, under a second account: https://stackoverflow.com/questions/47437332/so-i-want-the-answer-for-the-user-just-to-include-numbers-from-0-to-9-how-do-i/47439077#47439077 – Tom Lord Nov 22 '17 at 16:30

0 Answers0