Can't get If statement right

mainMenu = "Main Menu:

Phone Book Application
1.) Print Phone Book
2.) Add Entry
9.) Quit Application

Please select an option:"

print mainMenu

option = gets

( if option == 1
print “TODO: Print Phone Book”
print mainMenu
option = gets

 elsif option == 2
print "TODO: Add Entry"
print mainMenu
option = gets

 elsif option == 9
print "TODO: Quit Entry, type 'quit' to exit."
  quit.get
    ( if quit == "quit"
      abort("Goodbye.")
    else
      mainMenu
      option = gets
    end )

 else
print "Error, please enter 1, 2, or 9."
print mainMenu
option = gets

 end )

Every time I run this it just outputs “Error, please enter 1, 2 or 9.”
No matter what number I enter. What am I doing wrong?? Thanks!

gets will return a string and therefore option will be a string. “if
option
== 2” is expecting option to be an integer

A simple fix would be

option = gets.to_i

Okay, thanks! Now it is working. I figured it was thinking they were
integers, and I tried putting quotes around the 1, 2, and 9, but that
still didn’t work either.

But, now, the if statement in the option == 9 elsif section isn’t doing
anything?

And how would I get the program to just loop over and over until the
person quit?

You should be getting something like:

undefined local variable or method `quit’ for main:Object I’d suggest
examining that line to see what you’ve done wrong.

Lastly, when you use gets there will be a newline at the end of the
input.
For example,

irb(main):001:0> foo = gets
hello
=> “hello\n”

You can use Class: String (Ruby 2.0.0)
to
take care of it.

Well when gets returns the string it would still contain the “\n” at the
end of the line so you would be comparing “2\n” to “2” which will not
match. The .to_i will convert as much of the string it can into an
integer.
So a “2cats\n” will be converted to a 2. The moment .to_i encounters a
non
digit it stops so the “\n” from the “2\n” will be ignored.

Anyhow here is a quick hack on your program. It might be of some help

mainMenu = "Main Menu:

Phone Book Application
1.) Print Phone Book
2.) Add Entry
9.) Quit Application

Please select an option:"

option = “”

while option != “quit”
puts mainMenu

option = gets.chomp

case option
when “1”
puts “TODO: Print Phone Book”
when “2”
puts “TODO: Add Entry”
when “9”
puts “TODO: Quit Entry, type ‘quit’ to exit.”
print “>”
option = gets.chomp
else
puts “Error, please enter 1, 2, or 9. (you entered #{option})”
end
end

As for your loop quandary, you should try some of these options. I’ll
leave
it to you to decide which you like best:

or either of the solutions in the loops section of

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#Loops

I was wondering if I should switch to a case statement… But, I am way
more familiar with the way if, else statements work so I was trying that
first. Thanks a lot!

On Mon, Mar 4, 2013 at 10:15 PM, Peter H.
[email protected] wrote:

Ruby supports the if, else statements so it is fine to use it. It is just a
personal preference of mine. There is nothing “better” about using the case
statement (well none that I am aware of).

Maybe that “case” conveys the notion of several equivalent choices
while “if else…” is for more general control flow. In this case I’d
prefer “case” as well.

Kind regards

robert

Ruby supports the if, else statements so it is fine to use it. It is
just a
personal preference of mine. There is nothing “better” about using the
case statement (well none that I am aware of).