How to prevent user input error!
Author |
Message |
vdemons
|
Posted: Sun May 01, 2011 3:59 pm Post subject: How to prevent user input error! |
|
|
i would like to know how to prevent user input error,
for example;
var inputdelay:int
put "Please enter your disired time delay: "..
get inputdelay
if i initialize it to int and someone enters a string turing will automatically stop the program and pop the code window backup.
my question it if you can prevent that and output an error message and repeat the user input statement. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sun May 01, 2011 4:20 pm Post subject: RE:How to prevent user input error! |
|
|
You'll need 2 variables. A string, and an int. get the string, then use strintok() and strint() to convert it to an integer and store it in your integer variable. |
|
|
|
|
![](images/spacer.gif) |
HRI
|
Posted: Sat May 14, 2011 7:07 pm Post subject: Re: How to prevent user input error! |
|
|
Here's a piece of code that will get a string one character at a time, check to see whether it's a number, and then add it if it is. It's easy to set a length too.
Turing: |
setscreen ("graphics:max;max,offscreenonly") % max size and simply put, no flickering
var ch : string (1) := "" % holds entered character
var input : flexible array 1.. 0 of string (1) % dynamic array of characters
var entry : string
int delay
loop
if hasch % if key was pressed
then
getch (ch ) % read key into variable ch
exit when ch = KEY_ENTER % exit if enter key
if ch = KEY_BACKSPACE and upper (input ) > 0 % if backspace pressed and there's a character to delete
then
new input, upper (input ) - 1 % decrease array size (last character is erased)
put "\b" .. % put a backspace
elsif ord (ch ) >= ord ('0') and ord (ch ) <= ord ('9') % if another key was pressed between 0 and 9
then
new input, upper (input ) + 1 % increase array size
input (upper (input )) := ch % assign highest element to entered character
put ch .. % output entry
end if
View.Update % update screen
end if
end loop
for i: 1.. upper (input ) % move characters from array to string
entry (i ) := input (i )
end for
delay := strint (entry ) % convert string to integer
|
The reason I do it this way is because I simply don't know of a way to accomplish
Turing: |
var a : string := "abc"
a -= "c"
%or
a -= 1
|
Please give me a shout if you do :3
Alternatively, basically the last post put into code
I like the first way better because it doesn't allow other characters than numbers period, but the second works too. |
|
|
|
|
![](images/spacer.gif) |
|
|