Another newbie question

L

len

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler
 
D

david

Cit=E1t len said:
I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).
=20
How do I request input from the user such as:
=20
=20
I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.
=20
Thanks
Len Sumnler
=20
=20
=20


Well, this isn't BASIC, gets isn't supposed to display a prompt. Go like =
this:

puts ">>>Please enter dollar amount:"
amount =3D gets

David Vallner
 
D

David A. Black

--8323328-1636550047-1123853742=:12576
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1636550047-1123853742=:12576"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1636550047-1123853742=:12576
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Well, this isn't BASIC, gets isn't supposed to display a prompt. Go like = this:

puts ">>>Please enter dollar amount:"
amount =3D gets

Or use print instead of puts, if you want the input on the same line
as the prompt.


David

--=20
David A. Black
(e-mail address removed)
--8323328-1636550047-1123853742=:12576--
--8323328-1636550047-1123853742=:12576--
 
D

daz

len said:
How do I request input from the user such as:


I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.


STDOUT.sync=true
print '>> Please enter amount: $'
resp = STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"

#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?


daz
 
B

Brian Schröder

=20

=20
=20
STDOUT.sync=3Dtrue
print '>> Please enter amount: $'
resp =3D STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"
=20
#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?
=20
=20
daz
=20
=20
=20
=20
=20

Or use the readline library, there you can even supply the prompt and
you get a ton of extras.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
B

Bill Atkins

STDOUT.sync =3D true isn't necessary in that snippet. Even though you
haven't output a newline, the outbuffer will get flushed as soon as
you call gets.

Bill

=20

=20
=20
STDOUT.sync=3Dtrue
print '>> Please enter amount: $'
resp =3D STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"
=20
#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?
=20
=20
daz
=20
=20
=20
=20
=20


--=20
Bill Atkins
 
L

len

len said:
I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:


I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Thanks everyone for your responses. At this time, and with my level of
experience (none), I think the option I am looking for is the "puts"
command.

Again thanks everyone for your responses I have copied all of them for
future reference.

Len Sumnler
 
D

daz

Bill said:
STDOUT.sync = true isn't necessary in that snippet. Even though you
haven't output a newline, the outbuffer will get flushed as soon as
you call gets.


That's not a feature of Ruby 1.8.2
You may be describing *nix behaviour ?

On Windows, it looks as if the program is hanging unless #sync of #flush
is used.

[What's worrying is that I can't manage to spell acccount]


daz
 
D

David A. Black

Hi --

You'd probably want

puts "Please enter dollar amount":
gets line

I doubt it, unless you've got some reason to expect the input to
terminate with whatever line contains :)


David
 
B

Bill Atkins

That's strange - it doesn't hang on Windows for me.

=20
Bill Atkins wrote:
=20
STDOUT.sync =3D true isn't necessary in that snippet. Even though yo= u
haven't output a newline, the outbuffer will get flushed as soon as
you call gets.
=20
=20
That's not a feature of Ruby 1.8.2
You may be describing *nix behaviour ?
=20
On Windows, it looks as if the program is hanging unless #sync of #flush
is used.
=20
[What's worrying is that I can't manage to spell acccount]
=20
=20
daz
=20
=20
=20
=20
=20


--=20
Bill Atkins
 
D

David A. Black

--8323328-1305152715-1123865835=:16311
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1305152715-1123865835=:16311"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1305152715-1123865835=:16311
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

a minor correction:



- puts "Please enter dollar amount":
+ puts "Please enter dollar amount:"
gets line

I'm almost certain he actually wants:

line =3D gets


David

--=20
David A. Black
(e-mail address removed)
--8323328-1305152715-1123865835=:16311--
--8323328-1305152715-1123865835=:16311--
 
E

Ezra Zygmuntowicz

How do I request input from the user such as:



I would like the prompt to display and the user to be able to
enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.
]

Do this first:
$ sudo gem install highline

Then you can get nice prompts like this:


#!/usr/local/bin/ruby

require "rubygems"
require "highline/import"

dollar_amt = ask("Please enter the dollar amount : ")

Then you can just use dollar_amt however you want and when you rub
your program it will prompt for the input and once it gets it it will
assign it to dollar_amt. Nice and simple. Thanks to James and Greg.

HTH-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 
D

daz

David said:
I'm almost certain he actually wants:

line = gets


raise 'almost certain' # :)


------------------------------------------------------------ Kernel#gets
gets(separator=$/) => string or nil
------------------------------------------------------------------------
Returns (and assigns to $_) the next line from the list of files
in ARGV (or $*), or from standard input if no files are present
on the command line.
[...]


STDIN.gets is safer for this case, I think.


daz
 
D

David A. Black

--8323328-764081161-1123876667=:21614
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-764081161-1123876667=:21614"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-764081161-1123876667=:21614
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

I'm almost certain he actually wants:

line =3D gets


raise 'almost certain' # :)

------------------------------------------------------------ Kernel#gets
gets(separator=3D$/) =3D> string or nil
------------------------------------------------------------------------
Returns (and assigns to $_) the next line from the list of files
in ARGV (or $*), or from standard input if no files are present
on the command line.
[...]

Yes -- hence my original reply:

I doubt it, unless you've got some reason to expect the input to
terminate with whatever line contains :)
STDIN.gets is safer for this case, I think.

What would be the non-safeness of gets?


David

--=20
David A. Black
(e-mail address removed)
--8323328-764081161-1123876667=:21614--
--8323328-764081161-1123876667=:21614--
 
D

daz

David A. Black wrote
David said:
On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:

a minor correction:

On 12/08/05, Julian Leviston wrote:
You'd probably want

- puts "Please enter dollar amount":
+ puts "Please enter dollar amount:"
gets line

I'm almost certain he actually wants:

line = gets


raise 'almost certain' # :)

------------------------------------------------------------ Kernel#gets
gets(separator=$/) => string or nil
------------------------------------------------------------------------
Returns (and assigns to $_) the next line from the list of files
in ARGV (or $*), or from standard input if no files are present
on the command line.
[...]

Yes -- hence my original reply:

I doubt it, unless you've got some reason to expect the input to
terminate with whatever line contains :)


I wasn't referring to your 'separator' note.

What would be the non-safeness of gets?


David


Kernel#gets has a higher action when ARGV is non-empty.

#----------------------
ARGV.replace ["anyarg"]
line = gets # <--- STDIN.gets ---*
#----------------------

#-> C:/TEMP/rb80B3.TMP:2:in `gets': No such file or directory - anyarg (Errno::ENOENT)

Quoting the OP:
"I would like the prompt to display and the user to be able to enter the
data after the prompt."

No mention of: "... unless he supplies a C/L argument, in which case
treat it as a file and read the input from there instead."

Therefore:

daz
 
L

len

Thank you all.

Right now I just a newbie trying to write some simple programs to get
the hang of the language. For now

puts 'Please enter the bill amount'
amount = gets

will do nicely. One must learn to walk before you can run and jumping
tall building in a single bound.................maybe 18 months away:)

Len Sumnler
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top