# Why does my program exit?!

**URL:** <https://www.ruby-forum.com/t/why-does-my-program-exit/194834>\
**Category:** Ruby\
**Created:** [September 11, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834 "2010-09-11T19:37:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 11, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/1 "2010-09-11T19:37:28Z")

</div>

Hi I was hoping that anyone could help me with this piece of code from  
my program?! I have a menu with 2 submenus that you can reach from the  
main menu. When I start the program I want the main menu to be presented  
with a number of choices. My program is based on user input so naturally  
when you enter the program you should be able to do the choices that are  
presented in the menus. It worked before but now when I started adding  
features for it(its for an assignment for a course) now it just presents  
the menu and then exits the program. Can anyone help me with this. I  
will post the code below and hopefully someone with more programming  
skills than me can see the problem. Thanks!

```
module Menus

```

def self.getValidNumber  
input = gets.chomp

```
while input > @options.size || input < 1 do
  puts "cant do that try again."
  input = gets.chomp
end

number = input.to_f
if (number <= 0)
  puts "cant state a negative value."
  getValidPositiveNumber
end
return number

```

end

def self.get\_valid\_input(options)

```
input = gets.chomp

while (!options.include?(input) && !options.include?(input.to_i))
  puts "No good, you have to choose a value between " +

```

valid\_options.inspect  
input = gets.chomp  
end  
return input

end

class Menu

```
attr_reader :options

# Pass in array of options
def initialize(options)
  @options = options
end

def main_menu
  puts "---------------------------"
  puts " Main Menu"
  @options.each_with_index do |item, i|
    puts " #{i+1}. #{item}"
  end
  puts
  puts "What do you want to do?"
end

def self.make_choice(choice)
  # chooses something from the menu based on the choice
  case choice
  when 1
    check_in
  when 2
    check_out
  when 3
    puts $in_menu = lists_menu
  when 4
    puts $in_menu = economy_menu
  when 5
    puts "You are now leaving the camping, welcome back!"
    exit
  end
end

```

def lists\_menu  
puts “---------------------------”  
puts " List Menu"  
@options.each\_with\_index do |item, i|  
puts " #{i+1}. #{item}"  
end  
puts  
puts “What do you want to do?”  
end

def self.make\_choice(choice)  
case choice  
when 1  
puts $camping  
when 2  
puts $camping.history.all\_guests  
when 0  
$in\_menu = main.menu  
end  
end

def economy\_menu  
puts “---------------------------”  
puts " List Menu"  
@options.each\_with\_index do |item, i|  
puts " #{i+1}. #{item}"  
end  
puts  
puts “What do you want to do?”  
end

def self.make\_choice(choice)  
case choice  
when 1  
$camping.economy.printCosts  
when 2  
$camping.economy.enterNewPrices  
when 3  
puts $camping.history  
puts $camping.economy  
when 0  
$in\_menu = MAIN\_MENU  
end  
end

m = Menus::Menu.new [“Checkin”, “Checkout”,“Lists”,“Economy”,“Exit”]  
m.main\_menu  
if Menus == 3 then m = Menus::Menu.new [“List current guests”,“List all  
guests”,“back to main menu”]  
m.lists\_menu  
elsif Menus == 4 then m = Menus::Menu.new [“Price list”,“Edit price  
list”,“back to main menu”]  
m.economy\_menu  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 11, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/2 "2010-09-11T21:55:59Z")

</div>

Hi –

On Sun, 12 Sep 2010, Tim Romberg wrote:

> Hi I was hoping that anyone could help me with this piece of code from  
> my program?! I have a menu with 2 submenus that you can reach from the  
> main menu. When I start the program I want the main menu to be presented  
> with a number of choices. My program is based on user input so naturally  
> when you enter the program you should be able to do the choices that are  
> presented in the menus. It worked before but now when I started adding  
> features for it(its for an assignment for a course) now it just presents  
> the menu and then exits the program. Can anyone help me with this. I  
> will post the code below and hopefully someone with more programming  
> skills than me can see the problem. Thanks!

Let me turn the question around: why would you expect it not to exit? At  
what point are you telling it to wait for user input?

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally  
Compleat Philadelphia, PA, October 1-2, 2010  
Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 11, 2010, 10:25pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/3 "2010-09-11T22:25:51Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sun, 12 Sep 2010, Tim Romberg wrote:
> 
> > Hi I was hoping that anyone could help me with this piece of code from  
> > my program?! I have a menu with 2 submenus that you can reach from the  
> > main menu. When I start the program I want the main menu to be presented  
> > with a number of choices. My program is based on user input so naturally  
> > when you enter the program you should be able to do the choices that are  
> > presented in the menus. It worked before but now when I started adding  
> > features for it(its for an assignment for a course) now it just presents  
> > the menu and then exits the program. Can anyone help me with this. I  
> > will post the code below and hopefully someone with more programming  
> > skills than me can see the problem. Thanks!
> 
> Let me turn the question around: why would you expect it not to exit? At  
> what point are you telling it to wait for user input?
> 
> David
> 
> –  
> David A. Black, Senior Developer, Cyrus Innovation Inc.
> 
> The Ruby training with Black/Brown/McAnally  
> Compleat Philadelphia, PA, October 1-2, 2010  
> Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

Yes I know. I just were not sure how to incorporate the user input in a  
good way. Do you have any tips?!  
Regards.

---

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 11, 2010, 10:28pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/4 "2010-09-11T22:28:45Z")

</div>

Hi –

On Sun, 12 Sep 2010, Tim Romberg wrote:

> > > presented in the menus. It worked before but now when I started adding  
> > > features for it(its for an assignment for a course) now it just presents  
> > > the menu and then exits the program. Can anyone help me with this. I  
> > > will post the code below and hopefully someone with more programming  
> > > skills than me can see the problem. Thanks!
> > 
> > Let me turn the question around: why would you expect it not to exit? At  
> > what point are you telling it to wait for user input?
> 
> Yes I know. I just were not sure how to incorporate the user input in a  
> good way. Do you have any tips?!

Several, but I’m a bit skittish about directly answering questions that  
are for homework assignments without knowing the details a little more  
(whether it’s for a grade, etc.). Chalk it up to my many years as a  
professor 🙂

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally  
Compleat Philadelphia, PA, October 1-2, 2010  
Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 11, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/5 "2010-09-11T22:39:52Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sun, 12 Sep 2010, Tim Romberg wrote:
> 
> > > > presented in the menus. It worked before but now when I started adding  
> > > > features for it(its for an assignment for a course) now it just presents  
> > > > the menu and then exits the program. Can anyone help me with this. I  
> > > > will post the code below and hopefully someone with more programming  
> > > > skills than me can see the problem. Thanks!
> > > 
> > > Let me turn the question around: why would you expect it not to exit? At  
> > > what point are you telling it to wait for user input?
> > 
> > Yes I know. I just were not sure how to incorporate the user input in a  
> > good way. Do you have any tips?!
> 
> Several, but I’m a bit skittish about directly answering questions that  
> are for homework assignments without knowing the details a little more  
> (whether it’s for a grade, etc.). Chalk it up to my many years as a  
> professor 🙂
> 
> David
> 
> –  
> David A. Black, Senior Developer, Cyrus Innovation Inc.
> 
> The Ruby training with Black/Brown/McAnally  
> Compleat Philadelphia, PA, October 1-2, 2010  
> Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

ok, well before I was reading the input like this def self.menu  
puts “---------------------------”  
puts " Menu"  
puts " 1. Checkin"  
puts " 2. Checkout"  
puts " 3. List of current guests"  
puts " 4. List of all guests"  
puts " 5. Exit"  
puts “”  
puts " What do you want to do?"  
puts “---------------------------”  
print ": "  
choice = get\_input  
make\_choice(choice)  
end  
But with the current upgrade Im just receiving an undefined local  
variable or method. So I was thinking instead something like this:  
@options = gets.chomp. Is that stupid. I am terribly new at Ruby.

---

<div class="post-metadata">

**Author:** ![timrohde](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/94ad74/32.png) [@timrohde](https://www.ruby-forum.com/u/timrohde)\
**Post date:** [September 14, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/why-does-my-program-exit/194834/6 "2010-09-14T18:50:21Z")

</div>

On Sun, Sep 12, 2010 at 8:42 AM, Tim Romberg

But with the current upgrade Im just receiving an undefined local

> variable or method. So I was thinking instead something like this:

Please send us the _exact_ error message AND the line it is whinging  
about.  
That always helps.

The other major help is ALWAYS ALWAYS run ruby with the -w option and  
clean  
up all the warnings. (Hey, that is true for any language!)

–  
John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email :  
removed\_email\_address@domain.invalid  
New Zealand

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.
