Help with Calculator

ruby

#1

So I created a code to work as a calculator.

It says that the code doesn't work because sum is not defined. I defined it though in the beginning so it has a default value of 0 and in the def "start". Can anyone tell me what I did wrong?

def cal
#calculator vars-----------------------

sum = 0

#Calculators Defs----------------------


def start

puts "Give me a number please!"

num1 = Integer(gets.chomp)

puts ""

puts "Now I need another number"

num2 = Integer(gets.chomp)

puts ""
puts "Now what do you want to do with those numbers. Add(+), Subtract(-), Multiply(*) or Divide(/)?"
op = gets.chomp

until op == "*" or op == "+" or op == "/" or op == "-"
puts ""
puts "You need to choose one of the following! (*,/,-,+)"
op == gets.chomp
end

if op == "*"
	sum = num1 * num2
elsif op == "/"
	sum = num1 / num2
elsif op == "+"
	sum = num1 + num2
elsif op == "-"
	sum = num1 - num2
else 
	puts "Error! Something went wrong in the if statement in the start method!"
end

puts ""


end

def add
puts "Give me another number please!"
num2 = gets.chomp

sum = sum + num2


end

def multiply

puts "Give me another number please!"
num2 = gets.chomp

sum = sum * num2


end

def subtract

puts "Give me another number please!"
num2 = gets.chomp

sum = sum - num2


end

def divide

puts "Give me another number please!"
num2 = gets.chomp

sum = sum / num2


end



puts "///////////////////////////////"
puts "//        Calculator         //"
puts "///////////////////////////////"

puts "Welcome to the calculator!"

start

puts "The total of your numbers right now are #{sum}"

puts "Are you done with the program(Y/N)?"
title = gets.chomp

until title == "Y" or title == "y"

puts ""
puts "Pick what you want to do!"
puts "1 = Add on to the sum!"
puts "2 = Multiply another number with the sum!"
puts "3 = Subtract a number from the sum!"
puts "4 = Divide another number by the sum!"
puts "5 = Show the sum of numbers!"

abcd = Integer(gets.chomp)

until abcd == 1 or abcd == 2 or abcd == 3 or abcd == 4 or abcd == 5

puts ""
puts "Error! You have to choose by typing in 1, 2, 3, 4 or 5!"
abcd = Integer(gets.chomp)

end

if abcd == 1 
	add
elsif abcd == 2
	multiply
elsif abcd == 3
	subtract
elsif abcd == 4
	divide
elsif abcd == 5 
	puts "The sum of all your numbers is #{sum}"
else 
	puts ""
	puts "Error! Something went wrong in the if statement in the decide method!"
end

puts "Are you done with the program(Y/N)?"
title = gets.chomp

end


end

#2

Hi,

I haven't read everything, but I think here and at some other places you forgot to convert the input to an integer.