Why don't my variables add?


#1

Hi, total newbie here. Any help would be appreciated. I have tried to follow other examples but for some reason, I can not get the variable "tn" to be the result of adding "hn + hn + ssn" resulting in a new variable to be used in another section. No errors return, but the best I can get is for it to print the numbers entered as "10+2000+30"

I'm sure it's something simple and maybe I've been looking at it too long, but it won't work.
Thanks again
Carl

Make up any numbers

print "what is your house number?"
hn = gets.chomp
print "what is the last 4 digits of your phone number?"
pn = gets.chomp
print "what is the last 4 digits of you ssn?"
ssn = gets.chomp

tn = #{hn + pn + ssn}

puts """So if you add '#{hn} + #{pn} + #{ssn}', you get #{tn}."""


#2

What you would be adding is values, not variables, they just refer to values.
So what kind of values do you have after reading text from stdin?
What kind of values do you need in order to perform arithmetics?

Also, note that """ is an empty string and an opening quote (or a closing quote and then an empty string)


#3

Thanks for the response.
The values from gets.chomp would be numerical. So I though that since the numerical values would be assigned to the variables, that the variables would be numerical and could be added.
I also though that """ could be used in place of adding "" on every line.
Thanks again, appreciate it.
Carl


#4

gets.chomp returns text. Text can contain digits, but as far as ruby is concerned, it's still just text.

To add two pieces of text together is to put the second piece at the end of the first.


#5

Ahhh. Now that makes sense.
I'll work on it some more.

Thanks again for the prompt response.
Carl


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.