Anonymoose
Anonymoose

Reputation: 141

Optional digits in a sum?

This is my first programming language so please bear with me!

I can't quite figure out where it's going wrong. I'm not necessarily asking for a solution as this is a learning exercise; I just need a helping hand as to where I should be looking.

#Calculate the sum of two numbers and an optional third

#get first number
print "Please enter your first digit: "
value_1 = gets.chomp
print value_1

#get second number
print "Please enter your second digit: "
value_2 = gets.chomp

#get the additional number
print "Do you want to add an additional number?"
add_num_req = gets.chomp

#calculate result and put
if gets.chomp = "yes" || "Yes" || "YES"
    print "Please enter the additional digit: "
    add_num_1 = gets.chomp
    #print sum of three values
    print "Answer: " , (value_1.to_i + value_2.to_i + add_num_1.to_i), "\n";
else
    #print value_1 + value_2
    print "Answer: " , (value_1.to_i + value_2.to_i), "\n";
end

But this produces a blank return after putting in the response to the get.chomp for an additional digit. Any help?

Upvotes: 0

Views: 119

Answers (6)

Kimball
Kimball

Reputation: 1281

My guess is your problem is happening here:

#get the additional number
print "Do you want to add an additional number?"
add_num_req = gets.chomp

#calculate result and put
if gets.chomp = "yes" || "Yes" || "YES"

Since you are calling gets.chomp twice for the same input and using an assignment operator = in place of comparison operator ==. Also, as someone else has pointed out, each || operator should evaluate a Boolean expression, e.g. add_num_req == 'yes' || add_num_req == 'YES'. Without modifying your code too much, I think you want something like this:

print "Do you want to add an additional number? "
add_num_req = gets.chomp

#calculate results and put
if add_num_req.downcase == 'yes'
  # ...

On that note, if you plan to be evaluating a lot of strings, regular expressions are invaluable. I still can't write a decent regexp without checking a reference, but even so they make a world of difference!

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110725

Here's a more Ruby-like way to write your program:

def doit
  value_1 =  obtain_entry("Please enter your first digit: ").to_i
  value_2 =  obtain_entry("Please enter your second digit: ").to_i

  loop do
    case obtain_entry("Do you want to add an additional digit?: ")
    when "yes", "Yes", "YES"
      print "Please enter the additional digit: "
      puts "Answer: #{value_1 + value_2 + gets.to_i}"
      break
    when "no", "No", "NO"
      puts "Answer: #{value_1 + value_2}"
      break
    end
  end
end

def obtain_entry(str)
  print str
  gets.chomp
end

doit

A few points:

  • Since you are getting a response from the user more than once, put that in a method to which you pass the question to be asked (here, obtain_entry). For answers that are to be treated as integers, you may as well convert them to integers when they are returned. (In a real application you would of course want to make various checks on the type and reasonableness of answers.)
  • Do not define variables when it is not necessary to do so. For example, I've not created a variable for the reply to either of the questions "Do you want to add an additional digit?: " and "Please enter the additional digit: ".
  • I've added an endless loop that ensures that an acceptable "yes" or "no" answer is given to the question "Do you want to add an additional digit?. If an acceptable answer is given, we break out of the loop; if not, the question is repeated and the user is given another opportunity to answer.
  • It is often convenient to use a case statement in the way I have done when several replies result in the same action being taken.
  • When displaying output, I've used the more conventional way of forming a string, using string interpolation (e.g., #{variable_x}), as in puts "Answer: #{value_1 + value_2 + gets.to_i}". You must use double quotes for string interpolation to work.
  • When using IO#gets to obtain a string that is to be converted to an integer, String#chomp is not needed; gets.to_i is sufficient.

Upvotes: 0

SteveTurczyn
SteveTurczyn

Reputation: 36870

As a fourth alternative (and what I usually use) ...

if gets.chomp.downcase == "yes"

As with the regex match, it also accepts unexpected case arrangements (e.g. "yEs", "yES", "YeS" and so on)

Upvotes: 2

AShelly
AShelly

Reputation: 35600

A third alternative:

if ( ['yes','Yes','YES'].include?(add_num_req) )
    ...

Upvotes: 0

squiguy
squiguy

Reputation: 33380

In Ruby you can't compare a variable to many options as you have there. You have to do something like this:

if add_num_req == "yes" || add_num_req == "Yes" || add_num_req == "YES"

Another way to do it is to take advantage of the Enumerable module. But this is a little more advanced, although you will find it useful as you continue to use Ruby.

answers = ["yes", "Yes", "YES"]
if answers.any? { |e| add_num_req == e }

Upvotes: 1

bjhaid
bjhaid

Reputation: 9782

change:

if gets.chomp = "yes" || "Yes" || "YES" #you are using = instead of == which is equality

to:

if gets.chomp.match(/yes/i) #This is a case-insensitive regex to match "yes", "Yes" or "YES"

Upvotes: 0

Related Questions