Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# build a calculator interface that allows the user to input info to perform simple arithmetic.
# use the input operation and two numbers to provide the results.
# accept both the name and the symbol for each possible operation.

puts "\nWelcome to the CALCULATOR program, which operator would you like to use?"
puts """\nOptions:
1. add or +
2. subtract or -
3. multiply or *
4. divide or /"""

# handle unexpected input for operations and numbers.
# optional: add support for handling all cases (uppercase, capitals) for the operations.
# optional: output a message that informs the user that the input was invalid.
# optional: asks the user to re-enter the input for the same prompt.
# optional: does this until the input is valid.
# optional: uses the newer, valid input.

operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]

print "\nPlease choose one operator (name or symbol): "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using while and include? is a great way to check for valid user input.

option = gets.chomp.downcase

until operations.include?(option)
puts "INVALID INPUT, SORRY... Try again.\n"
print "\nPlease choose one operator (name or symbol): "
option = gets.chomp.downcase
end

# handle unexpected input for numbers (expected 0, positive and negative numeric input).
# Optional: output a message to the command line that informs the user that the input was invalid.
# Optional: asks the user to re-enter the input for the same prompt.
# Optional: does this until the input is valid.
# Optional: uses the newer, valid input.

puts "\nAttention! This CALCULATOR works with two numbers at a time."

print "Please input the FIRST number: "
Copy link

@beccaelenzil beccaelenzil Aug 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method and/or a loop?

num1 = gets.chomp
while (num1.to_i.to_s != num1.strip)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a clever way to check for valid user input

puts "\nINVALID INPUT, SORRY... Try again."
print "Please input the FIRST number: "
num1 = gets.chomp
end
num1 = num1.to_f

print "Please input the SECOND number: "
num2 = gets.chomp
while (num2.to_i.to_s != num2.strip)
puts "\nINVALID INPUT, SORRY... Try again."
print "Please input the SECOND number: "
num2 = gets.chomp
end
num2 = num2.to_f

# handle divide when attempting to divide by zero (just exit the program).
# make your program know when it needs to return an integer versus a float.

if option == "add" || option == "+"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a case/when block to simplify your code.

puts "\nTOTAL: #{(num1 + num2)}"
elsif option == "subtract" || option == "-"
puts "\nTOTAL: #{(num1 - num2)}"
elsif option == "multiply" || option == "*"
puts "\nTOTAL: #{(num1 * num2)}"
elsif option == "divide" || option == "/"
if num2 == 0
puts "You can't divide by 0. Sorry...\n"
exit
else
puts "\nTOTAL: #{(num1 / num2)}"
end
end

puts "\n"