Reputation: 1617
So far I've got
puts "Enter a calculation: "
calc = gets.chomp
def add(a, b)
puts a + b
end
puts add(calc)
And now I'm ashamed to admit but I'm stumped, I've tried writing add methods etc... but I just can't seem to wrap my head around getting this to calculate and output the correct results.
To simplify this, how can I get the ability to add working?
I.E user enters calculation (2 integers), program adds the calculation, program outputs results, program asks for another calculation.
Upvotes: 0
Views: 4422
Reputation: 21
I know this is a bit older post, but people do still find this answer and I want to add to what jkrmr said above.
The code jkrmr posted is great but does not handle floating point calculations and that was an easy fix so I added that functinoality. :-)
#! /usr/bin/ruby
def calc
puts "Calculator 1.1 \nEnter 'q' to quit."
while true
print ">> "
str = gets.chomp.split(" ") # splits into array, rejects blanks
return if str[0] == 'q' # quit if first element is 'q'
if str[0].include? "."
operand1 = str[0].to_f
else
operand1 = str[0].to_i
end
operator = str[1].to_sym
if str[2].include? "."
operand2 = str[2].to_f
else
operand2 = str[2].to_i
end
case operator
when :+ then puts operand1 + operand2
when :- then puts operand1 - operand2
when :* then puts operand1 * operand2
when :/ then puts operand1 / operand2
when :% then puts operand1 % operand2
else
puts "invalid input"
end
end
end
if __FILE__ == $0 # if run as script, start calc: see http://j.mp/HOTGq8
calc
end
Upvotes: 2
Reputation: 2236
I think this kind of script is perfect for a case statement. Here's a first pass that works with binary operators:
#! /usr/bin/ruby
def calc
puts "Calculator 1.0 \nEnter 'q' to quit."
while true
print ">> "
str = gets.chomp.split(" ") # splits into array, rejects blanks
return if str[0] == 'q' # quit if first element is 'q'
operand1 = str[0].to_i
operand2 = str[2].to_i
operator = str[1].to_sym
case operator
when :+ then puts operand1 + operand2
when :- then puts operand1 - operand2
when :* then puts operand1 * operand2
when :/ then puts operand1 / operand2
when :% then puts operand1 % operand2
else
puts "invalid input"
end
end
end
if __FILE__ == $0 # if run as script, start calc: see http://j.mp/HOTGq8
calc
end
Then, at the command line:
$ ./calc.rb
Calculator 1.0
Enter 'q' to quit.
>> 55 / 5
11
>> 90 / 10
9
>> 5 * 3093
15465
Good luck!
These are great resources if you're just starting out: RubyMonk Codecademy
Upvotes: 3
Reputation: 1827
To build on that. If you want to continue to recieve calculation requests you can put the process in a loop(among many solutions).
while true
puts 'Enter Val 1'
v1 = gets.chomp.to_i
puts 'Enter Val 2'
v2 = gets.chomp.to_i
puts "#{v1} + #{v2} = #{v1+v2} "
end
Upvotes: 1
Reputation: 13344
Here's a quick little calculator I whipped up to help you get started:
#! /usr/bin/ruby
def add(a, b)
a + b
end
while(true) do
puts "Enter a calculation: "
# chomp off the \n at the end of the input
calc = gets.chomp
# quit if the user types exit
exit if calc.include?("exit")
# puts the result of the add function, taking input of calc "split" into two numbers
puts add(calc.split("").first.to_i, calc.split("").last.to_i)
# spacing
puts
end
Upvotes: 1
Reputation: 1904
Just think of your problem one step at a time. You want the user to provide to integers. So start with a simple prompt, like you have already done:
puts "Enter your first value"
Now get the value from the user:
firstVal = gets.chomp
Now provide another prompt, and get a second value.
puts "Enter your second value"
secVal = gets.chomp
And output your results:
puts "#{firstVal} + #{secVal} = #{firstVal.to_i + secVal.to_i}"
Sometimes just writing it out plain and simple is the easiest first step. Now you can create an add function to do this more efficiently. Try it out, and see if you have any luck!
EDIT:
Also, I noticed your add
function takes two parameters, but you are only passing it one.
In order to call a function with two parameters, you need two values to provide it with. For example:
x = 5
y = 2
def add(a, b)
return a + b
end
puts add(x, y)
Upvotes: 2