zCoding
zCoding

Reputation: 35

How to stop program from exiting itself out?

my question regards the programming language of Ruby and the Ruby program.

After using this code:

print "What's your first name?" 
first_name = gets.chomp
first_name.capitalize!

print "What's your last name?" 
last_name = gets.chomp
last_name.capitalize!

print "What city are you from?" 
city = gets.chomp
city.capitalize!

print "What state or province are you from?" 
state = gets.chomp
state.upcase!

print "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}!"

After the program asks for the state or province and I enter in a value, the program automatically exits itself out.

I am using RubyInstaller for Windows (version Ruby 1.9.3-p545). I used Notepad++ to type the code and saved it under the Ruby .rb extension.

I ran the program by double-clicking the file from where I saved it.

Thanks! :)

Upvotes: 2

Views: 2411

Answers (5)

nikolayp
nikolayp

Reputation: 17919

Infinite loop still works

while true
  'something'
end

Upvotes: 0

user6078475
user6078475

Reputation: 1

print"What's your first name?" first_name=gets.chomp first_name.capitalize!

print"What's your last name?" last_name=gets.chomp last_name.capitalize!

print"What city are you from?" city=gets.chomp city.capitalize!

print"What state are you from?" state=gets.chomp state.upcase!

puts"Your name is #{first_name},your last name is #{last_name}, and your from #{city},#{state}!"

Works

Upvotes: -1

Maged Makled
Maged Makled

Reputation: 1986

have you tried from your command to run

irb code.rb

Assuming your file name is code.rb

Also as suggests try puts instead of print

Upvotes: 0

joelparkerhenderson
joelparkerhenderson

Reputation: 35443

One easy way is to add this at the end:

puts "Press RETURN when you're done."
gets

Upvotes: 6

Amadan
Amadan

Reputation: 198304

Well, it is the end of the program, there are no more user questions to be answered at that point. So what do you want your program to do after it asks you for state/province and repeats your data to you?

You might want to change print to puts though.

EDIT: As Michael and Leo say in comments, if you start your program by double-clicking the file in Windows, it will open a terminal, run your program, and close the terminal as soon as the program is done. The program is repeating the information back in a sentence with the valoues in the sentence; but that information is on your screen only for a split second, before Windows closes your program's terminal. Leo and Michael each suggest a way (run from terminal or insert a sleep statement); another way similar to Leo's is having another gets at the end and asking the user to hit Enter when they are done reading.

Upvotes: 1

Related Questions