1. RE: DOS: A newbie question about get()

Alex Caracatsanis wrote:
> 
> 
> I have a beginner's question about get(0).
> 
> I tried to use get(0) to read and check user input from the keyboard...

Just use gets(0). And do away with the rest of the code, since 
keyboard input can't fail. Either something gets typed in, or 
it doesn't. And the user won't have to type the quotes.

Irv

new topic     » topic index » view message » categorize

2. RE: DOS: A newbie question about get()

Hi Alex,

What do you want to accomplish?  Do you want to check input per 
character as it's being typed?  Do you just want want to get a whole 
string of characters (without having to use quotes) and then check that 
it's valid?  prompt_string() might be what you're after but it's hard to 
say with the info you've provided.

Example:
----------
include get.e
sequence got

got = prompt_string( "Enter something here: " )
printf(1,"You entered \"%s\".\n",{got})
----------
-- Brian
----------

Alex Caracatsanis wrote:
> 
> 
> I have a beginner's question about get(0).
> 
> I tried to use get(0) to read and check user input from the keyboard,
> and I anticipated that if it returned {GET_FAIL, 0}, then it would be a
> simple matter for the program to loop and ask the user to input a new
> value. I was surprised to find that if the user entered, say, bad
> (instead of "bad") then the program automatically looped 3 times - once
> for each character - and I couldn't find a way to make it loop only the
> once.
> 
> Is it possible to get around this behaviour in a simple way? Secondly,
> am I perhaps using the wrong tool for the job (of reading and checking
> keyboard input) - maybe gets() or getc() etc would be better?
> 
> Here's what I did....
> 
> include get.e
> sequence got
> 
> while 1 do
>     puts(1, "\nEnter something here (enclose words in \" \"): ")
>     got = get(0)
>     
>     if got[1] = 1 then
> 	puts(1, "\nInvalid entry - try again")
>     elsif got[1] = -1 then
> 	puts(1, "\nEOF reached before read - try again")
>     else
> 	puts(1, "\nValid entry - get() returned ") print(1, got)
> 	exit
>     end if
> end while
> 
> Thank you
> 
> Alex Caracatsanis
> 
>

new topic     » goto parent     » topic index » view message » categorize

3. RE: DOS: A newbie question about get()

Hi Brian, and others:

-----Original Message-----
From: Brian Broker [mailto:bkb at cnw.com] 

What do you want to accomplish?  

Fair point, Brian! 

I realize now that I was trying to understand what get() does, how it
works, and where it stands in comparison with the other routines
available for reading input. So I set myself an artificial problem: if
my program had to read input that may be either a number or a string,
but that must not be a no_input or invalid_input, how would I implement
it using get(); and would get() be the best way of implementing it in
any case? (Maybe the problem would've been more convincing if I'd set
out to read data from a file.)

I realized that prompt_string() and prompt_number() will read their
respective data types, and that gets() won't read numbers; and I
understood that getc() will read the next character (altho' come to
think of it, I don't really understand in what context this might be a
useful thing to do). It seemed that get() was the ideal tool to read
either data type, and do a bit of error-checking at the same time. I
tried to write some code to put it thro' its paces, so to speak.

Maybe I should've asked: when's it best to use get()? 
...or: in what programming tasks would get() be ideal?
...or even: if get() didn't exist, would it be missed?

Thank you for your advice

Alex Caracatsanis

new topic     » goto parent     » topic index » view message » categorize

4. RE: DOS: A newbie question about get()

I think the documentation explains very well the advantages of using 
get():

"get() can read arbitrarily complicated Euphoria objects. You could have 
a long sequence of values in braces and separated by commas, e.g. {23, 
{49, 57}, 0.5, -1, 99, 'A', "john"}. A single call to get() will read in 
this entire sequence and return it's value as a result."

"The combination of print() and get() can be used to save a Euphoria 
object to disk and later read it back."

So I wouldn't use it to read from the keyboard but rather from a file 
that was written using print().  I like games so I find this to be the 
easiest way to write and store a high score table or saved game stats 
for instance.  You don't have to worry about the details of reading in 
your data.  You get() back exactly what you print().

-- Brian

Alex Caracatsanis wrote:
> 
> 
> Hi Brian, and others:
> 
> -----Original Message-----
> From: Brian Broker [mailto:bkb at cnw.com] 
> 
> What do you want to accomplish?  
> 
> Fair point, Brian! 
> 
> I realize now that I was trying to understand what get() does, how it
> works, and where it stands in comparison with the other routines
> available for reading input. So I set myself an artificial problem: if
> my program had to read input that may be either a number or a string,
> but that must not be a no_input or invalid_input, how would I implement
> it using get(); and would get() be the best way of implementing it in
> any case? (Maybe the problem would've been more convincing if I'd set
> out to read data from a file.)
> 
> I realized that prompt_string() and prompt_number() will read their
> respective data types, and that gets() won't read numbers; and I
> understood that getc() will read the next character (altho' come to
> think of it, I don't really understand in what context this might be a
> useful thing to do). It seemed that get() was the ideal tool to read
> either data type, and do a bit of error-checking at the same time. I
> tried to write some code to put it thro' its paces, so to speak.
> 
> Maybe I should've asked: when's it best to use get()? 
> ...or: in what programming tasks would get() be ideal?
> ...or even: if get() didn't exist, would it be missed?
> 
> Thank you for your advice
> 
> Alex Caracatsanis
> 
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu