
-----------------------------------
tjmoore1993
Mon Apr 27, 2009 7:04 pm

[Help] Get Statements
-----------------------------------
What is it you are trying to achieve?
I am trying to get a reply from a user but only limiting them to entering a max of 8 characters.

Describe what you have tried to solve this problem
Honestly, I was not able to try things because I had no clue where to start.

Please specify what version of Turing you are using
I am using version 4.xx


To summarize this, If it is possible to stop input after a certain ammount of characters. Please share what you know.

Example:
Rodney buttface < This is 15 characters long
I want this
Rodney b < 8 characters long

-----------------------------------
Tony
Mon Apr 27, 2009 7:12 pm

RE:[Help] Get Statements
-----------------------------------
read up documentation on [tdoc]get[/tdoc]
[code]
get foo:8
[/code]
Although that will not prevent them from typing more than 8 characters, just the reading part (and the rest will be left over in a buffer).

Alternatively you could write your own read-buffer loop with [tdoc]getch[/tdoc]

-----------------------------------
tjmoore1993
Mon Apr 27, 2009 7:38 pm

RE:[Help] Get Statements
-----------------------------------
Problem solved thanks to Tony's excellent resourcing skills. :)


setscreen ("graphics")

var name : string := ""
var x : int := 0

procedure getKey
    var ch : string (1)
    getch (ch)
    name += ch
    cls
end getKey

for i : 1 .. 8
    x := x + 1
    locate (1, x)
    getKey
    put name
end for


-----------------------------------
Tony
Mon Apr 27, 2009 7:43 pm

RE:[Help] Get Statements
-----------------------------------
bonus Tonys-approval points will be given out for implementing the use of backspace ;)

-----------------------------------
tjmoore1993
Mon Apr 27, 2009 8:06 pm

Re: RE:[Help] Get Statements
-----------------------------------
I will work on it a bit more. That was just a simple fix :)

-----------------------------------
Euphoracle
Mon Apr 27, 2009 10:08 pm

RE:[Help] Get Statements
-----------------------------------
Problem now is that people can't back-space errors, right?

-----------------------------------
Tony
Mon Apr 27, 2009 10:23 pm

RE:[Help] Get Statements
-----------------------------------
and that one would have to type out a bunch of spaces, to enter a word less than 8 characters.

-----------------------------------
Dusk Eagle
Mon Apr 27, 2009 10:31 pm

Re: [Help] Get Statements
-----------------------------------
How about this Tony?

var name : string := ""
var x : int := 0
const BACKSPACE := 8

fcn getKey (word:string):string
    var ch : string (1)
    getch (ch)
    cls
    if ord(ch) = BACKSPACE then
        result word (1..length(word)-1) %returns all letters in word except the last one.
    else
        result word+ch
    end if
end getKey

loop
    exit when length(name) = 8
    locate (1, length(name)+1)
    name := getKey(name)
    put name
end loop

put name


-----------------------------------
Tony
Mon Apr 27, 2009 10:54 pm

RE:[Help] Get Statements
-----------------------------------
Crashes the program if you try to backspace on an empty string.

-----------------------------------
Dusk Eagle
Mon Apr 27, 2009 11:01 pm

Re: [Help] Get Statements
-----------------------------------
Well that's not hard to fix:
var name : string := ""
var x : int := 0
const BACKSPACE := 8

fcn getKey (word : string) : string
    var ch : string (1)
    getch (ch)
    cls
    if ord (ch) = BACKSPACE then
        if length (word) = 0 then
            result word
        else
            result word (1 .. length (word) - 1) %returns all letters in word except the last one.
        end if
    else
        result word + ch
    end if
end getKey

loop
    exit when length (name) = 8
    locate (1, length (name) + 1)
    name := getKey (name)
    put name
end loop


Are there any other errors with this program? Because I can't find any.

-----------------------------------
Tony
Mon Apr 27, 2009 11:39 pm

RE:[Help] Get Statements
-----------------------------------
I think that takes care of the backspace. :)

-----------------------------------
tjmoore1993
Tue Apr 28, 2009 2:20 pm

Re: [Help] Get Statements
-----------------------------------
Well that's not hard to fix:
var name : string := ""
var x : int := 0
const BACKSPACE := 8

fcn getKey (word : string) : string
    var ch : string (1)
    getch (ch)
    cls
    if ord (ch) = BACKSPACE then
        if length (word) = 0 then
            result word
        else
            result word (1 .. length (word) - 1) %returns all letters in word except the last one.
        end if
    else
        result word + ch
    end if
end getKey

loop
    exit when length (name) = 8
    locate (1, length (name) + 1)
    name := getKey (name)
    put name
end loop


Are there any other errors with this program? Because I can't find any.
Error:
You can not backspace when max characters are entered.

Request:
If possible could you make it work so if the key enter is pushed it will do an action  but still maintain that 8 char limit.

This really means a lot!
