Arrays - Storing User Input

W

woodyee

Easy one but I can't figure this out! I'm trying to ask someone to
type some stuff, and then at the end (when they press the spacebar
without typing anything), I want to display what they typed.

Here's my code:


something = []
puts "Say something..."
something = gets.chomp
puts something

I'm seeing what I typed after I hit the spacebar. Any ideas? Thanks!!
 
J

John Joyce

space bar?
you mean enter or return?
It does exactly what it should do.
you input , it outputs the same thing immediately.
What is it that you are expecting it to do?
 
B

Brian Candler

Easy one but I can't figure this out! I'm trying to ask someone to
type some stuff, and then at the end (when they press the spacebar
without typing anything), I want to display what they typed.

Here's my code:


something = []
puts "Say something..."
something = gets.chomp
puts something

I'm seeing what I typed after I hit the spacebar. Any ideas? Thanks!!

You mean 'Enter' rather than 'Spacebar' ?

For Linux, you can use the ruby-termios library (from RAA) to turn echo
off/on, or highline (also from RAA) for a higher-level interface.
http://raa.ruby-lang.org/project/highline/

Under Windows I have no idea.
 
W

woodyee

(blushing) Yes; I want to press Enter.

I want to be able to press Enter without having typed anything in
order to get back what I typed. The way it is now, I'm only able to
enter info on 1 line. I want to be able to type, hit enter, type
something and hit enter, etc, hit enter without typing and then I'll
see all of those lines that I typed.
 
S

Stefano Crocco

Alle venerd=EC 9 marzo 2007, woodyee ha scritto:
(blushing) Yes; I want to press Enter.

I want to be able to press Enter without having typed anything in
order to get back what I typed. The way it is now, I'm only able to
enter info on 1 line. I want to be able to type, hit enter, type
something and hit enter, etc, hit enter without typing and then I'll
see all of those lines that I typed.

If I understand correctly, the behavior of your program should be like this:
Say something text1 [press enter]
Say something text2 [press enter]
Say something [press enter]
text1
text2

Then, the following code should do the job:

something=3D[]
loop do #iterates until explicitly told to stop
puts "Say something..."
res=3Dgets.chomp #read the line from the keyboard
if res.empty? #if the line is empty, the user has only pressed enter, so
puts something #display what he already wrote
break #exit the loop
else something << res #the user has entered some text, so store it
end
end

Essentially, your code had two problems:
1- it lacked the enclosing loop, so the code would have been executed only=
=20
once
2- instead of storing the string into the array, you replaced the array wit=
h=20
the string

I hope this helps
 
J

John Browning

perhaps:

something = []
puts "Say something..."
loop do
nextline = gets.chomp
something << nextline
break if nextline == ""
end
puts something
 
J

John Joyce

Oh! Well, friend, you need more program.
You need a loop and a prompt or some exit mechanism.
The most basic of a program like this is loop that waits to be told
to quit, or told to do other things.
So you want to put that stuff inside a loop that waits for a
condition to exit and (perhaps another condition to) give output.
here is some pseudo code, you'll do the not-so-heavy lifting...

while condition is
prompt for input
while another condition is
get input
continue or end
get input command for output
prompt for more?
continue or end
end

Pretty much you will see this in any language. Definitely can be done
more gracefully.
Definitely can have more features.
Questions start to arise...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top