From: Peter Hickman Date: 2013-03-05T04:36:02+09:00 Subject: Re: Can't get If statement right --047d7b343e8a7d5f6b04d71e77dc Content-Type: text/plain; charset=ISO-8859-1 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 --047d7b343e8a7d5f6b04d71e77dc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Well when gets returns the string it would still contain t= he "\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 mu= ch of the string it can into an integer. So a "2cats\n" will be c= onverted to a 2. The moment .to_i encounters a non digit it stops so the &q= uot;\n" from the "2\n" will be ignored.

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

mainMenu =3D "Main Menu:

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

=A0 =A0 Ple= ase select an option:"


option =3D "= ;"

while optio= n !=3D "quit"
=A0 puts mainMenu

<= /div>
=A0 option =3D gets.chomp

=A0 case option
=A0 when "1"
=A0 put= s "TODO: Print Phone Book"
=A0 when "2"
=A0 =A0 puts "TODO: Add Entry"
=A0 when "9"
=A0 =A0 puts "TODO: Quit Entry, type 'quit' t= o exit."
=A0 =A0 print ">"
=A0 =A0 option =3D gets.chomp
=A0 else
=A0 =A0 puts "Error, plea= se enter 1, 2, or 9. (you entered #{option})"
=A0 end
end

--047d7b343e8a7d5f6b04d71e77dc--