Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to prevent user input error!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vdemons




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
HRI




PostPosted: 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
Turing:

var entry : string
var delay := int

loop
   get entry
   exit when strintok (entry) = true
end loop

delay := strint (entry)

I like the first way better because it doesn't allow other characters than numbers period, but the second works too.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: