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?
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 2:20 PM, Kat VS <lists@ruby-forum.com> wrote:
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?
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
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).
On Mon, Mar 4, 2013 at 2:20 PM, Kat VS <lists@ruby-forum.com> wrote:
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?
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
···
On Mon, Mar 4, 2013 at 10:15 PM, Peter Hickman <peterhickman386@googlemail.com> 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).