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
42 changes: 42 additions & 0 deletions calculator_proj.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
puts "Welcome to the calculator"
puts

puts "The calculator can add,subtract,divide and multiply."

puts "Please select your choice "

choice = gets.chomp
if choice == "add" || choice == "+"
puts "You chose addition"
elsif
choice == "subtract" || choice == "-"
puts "You chose subtraction"
elsif
choice == "multiply" || choice == "*"
puts " You chose multiplication"
elsif
choice == "divide" || choice == "/"
puts "You chose division"
else
puts " Calculator does not accept that input."

Choose a reason for hiding this comment

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

Instead of just an if could you wrap this in a loop, until they entered a choice on the list? We did something similar in the textbook notes complex conditionals.

end

puts "Please give me your first number"
first_num = gets.to_f

Choose a reason for hiding this comment

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

Similarly, is there anything you could do to ensure that the user enters a number?

num = ""
until num.to_i.to_s == num
  puts "Please enter a number"
  num = gets.chomp
end

puts "Please give me your second number"
second_num = gets.to_f


if (choice == "add" || choice == "+")
total_add = first_num + second_num
puts total_add
elsif (choice == "subtract" || choice == "-")
total_subtract = first_num - second_num
puts total_subtract
elsif (choice == "multiply" || choice == "*")
total_multiply = first_num * second_num
puts total_multiply
elsif (choice == "divide" || choice == "/")
total_divided = first_num / second_num
puts total_divided
end