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
49 changes: 49 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@


puts "Welcome to the operator program! which operator would you like to use?"

operatormenu =["add(+)","subtract(-)","multiply(*)","divide(/)"]

def printmenu(arr)
4.times do |i|
puts "#{i+1}. #{arr[i]}"
end
Comment on lines +8 to +10

Choose a reason for hiding this comment

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

Indentation is needed here.

end

printmenu(operatormenu)

puts "which operation would you like to carry on?"
x = gets.chomp
options = ["add", "+","subtract","-","multiply","*","divide","/"]
if !options.include?(x)
puts "wrong input! try again!"
x = gets.chomp

Choose a reason for hiding this comment

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

indentation

end


puts "what numbers would you like to calculate"
a= gets.chomp.to_i

Choose a reason for hiding this comment

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

Don't use variable names like a. Instead use meaningful variable names like first_num

while !options.include?(a==Integer)

Choose a reason for hiding this comment

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

This doesn't work.

Suggested change
while !options.include?(a==Integer)
while a != a.to_s.to_i

puts "wrong input! Please enter an Integer .. try again!"
a=gets.chomp.to_i
end
b= gets.chomp.to_i
while !options.include?(b==Integer)

Choose a reason for hiding this comment

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

Suggested change
while !options.include?(b==Integer)
b != b.to_s.to_i```

puts "wrong input! Please enter an Integer .. try again!"

Choose a reason for hiding this comment

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

Suggested change
puts "wrong input! Please enter an Integer .. try again!"
puts "wrong input! Please enter an Integer .. try again!"
b = gets.chomp.to_i

end

def operator(m,n,y)
case y

Choose a reason for hiding this comment

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

The case should be indented under the method here.

when "add","+"

Choose a reason for hiding this comment

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

The when should be indented under the case.

return "the result is #{m+n}"
when "subtract","-"
return "the result is #{m-n}"
when "multiply","*"
return "the resutl is #{m*n}"
when "divide","/"
return "the result is #{m/n}"
end
end

puts operator(a,b,x)