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
109 changes: 109 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# calculator program that allows user to choose what operator, along with two numbers to calculate.

# the program will run once while true and if the user wants to play again, they would need to type 'y' when prompted
play_again = "y"


# while will run true the first time and begin the program
while play_again == "y"

puts """
Welcome to the calculator program. Which operator would you like to use?
1. add(+)
2. subtract(-)
3. multiply(*)
4. divide(/)

"""

# prompts the user to select which operator option they'd like to use.
print "Please choose one operator (name or symbol): "
user_input = gets.chomp

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think there might be a more specific name for this variable than user_input, since there are several pieces of user input in this program. Something like operation might be a better choice.


# verifies if user enters in one of the defined options and if not, asks the user to re-enter their choice.
until %[add + subtract - multiply * divide /].include?(user_input)
print "Your input is invalid. Please try again. "
user_input = gets.chomp

Copy link
Copy Markdown

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?

end


# user is asked to enter in their first integer
print "Enter the first number: "
first_number = gets.chomp

# created an until loop to verify if user enters in an integer, and if not, prompts them to try again.
# found this solution located here: https://stackoverflow.com/a/1236045 would like to know more on how
# rescue works or if there's a more ideal solution.

until (Integer(first_number) rescue false)
print "invalid input, please enter a number: "
first_number = gets.chomp
end


# same code as first number prompt
print "Enter the second number: "
second_number = gets.chomp

until (Integer(second_number) rescue false)
print "invalid input, please enter a number: "
second_number = gets.chomp
end


# saving the input variables to integer format so the calculations run correctly
first_number = first_number.to_i
second_number = second_number.to_i


# created a case statment to run through the user_input operator choice and depending on
# which operator is selected, the calculation is completed and displayed for the user.
case user_input

when "add", "+"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This code isn't repeated, but I think it would still increase readability to wrap this case/when section in a method. That would clearly delineate where it starts and ends, and make it explicit what data it needs to work. The method signature might be something like perform_calculation(user_input, first_number, second_number).


result_add = first_number + second_number
puts "Your result is: #{first_number} + #{second_number} = #{result_add}"

when "subtract", "-"

result_sub = first_number - second_number
puts "Your result is: #{first_number} - #{second_number} = #{result_sub}"

when "divide", "/"

# ensuring that if the user enters in 0 for their second number, the program asks for a new number if
# division operator is selected because you cannot divide by zero.
# Converts their number to a float to get accurate division result.
if second_number == 0
print "Cannot be divided by zero. Please re-enter your option: "
second_number = gets.chomp.to_f
end

result_div = first_number / second_number
puts "Your result is: #{first_number} / #{second_number} = #{result_div}"

when "multiply", "*"

result_multi = first_number * second_number
puts "Your result is: #{first_number} * #{second_number} = #{result_multi}"

end


# asks the user if they want to run the program again
print "Want to play again? (y/n): "
play_again = gets.chomp

# if 'y' is entered, the loop will run the program again, if neither y nor n is entered,
# the user will be asked to type again.
until play_again == "y" || play_again == "n"
print "invalid, please use 'y' or 'n'"
play_again = gets.chomp
end

end

puts
# if n is chosen, the program ends and thanks the user.
puts "Thanks for playing!"