Why does my program exit?!

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

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

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

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

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 :slight_smile:

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

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 :slight_smile:

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

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.

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 :
[email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs