From: Peter Hickman Date: 2013-03-05T04:02:33+09:00 Subject: Re: Can't get If statement right --047d7b66fc19bcdbca04d71dff1e Content-Type: text/plain; charset=ISO-8859-1 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 On 4 March 2013 18:58, Kat VS wrote: > 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! > > -- > Posted via http://www.ruby-forum.com/. > > --047d7b66fc19bcdbca04d71dff1e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
gets will return a string and therefore option will be a s= tring. "if option =3D=3D 2" is expecting option to be an integer<= div>
A simple fix would be

option =3D gets.to_i
=A0


On 4 March 2013 18:58, Kat VS <lists@ruby-forum.com> wrote:
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 Please select an option:"

print mainMenu

option =3D gets

( if option =3D=3D 1
=A0 =A0 print "TODO: Print Phone Book"
=A0 =A0 print mainMenu
=A0 =A0 option =3D gets

=A0 =A0 =A0elsif option =3D=3D 2
=A0 =A0 print "TODO: Add Entry"
=A0 =A0 print mainMenu
=A0 =A0 option =3D gets

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

=A0 =A0 =A0else
=A0 =A0 print "Error, please enter 1, 2, or 9."
=A0 =A0 print mainMenu
=A0 =A0 option =3D gets

=A0 =A0 =A0end )




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

--
Posted via http://= www.ruby-forum.com/.


--047d7b66fc19bcdbca04d71dff1e--