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

Username:   Password: 
 RegisterRegister   
 [Help] Get Statements
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tjmoore1993




PostPosted: Mon Apr 27, 2009 7:04 pm   Post subject: [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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Apr 27, 2009 7:12 pm   Post subject: RE:[Help] Get Statements

read up documentation on get
code:

get foo:8

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 getch
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
tjmoore1993




PostPosted: Mon Apr 27, 2009 7:38 pm   Post subject: RE:[Help] Get Statements

Problem solved thanks to Tony's excellent resourcing skills. Smile

Turing:

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




PostPosted: Mon Apr 27, 2009 7:43 pm   Post subject: RE:[Help] Get Statements

bonus Tonys-approval points will be given out for implementing the use of backspace Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
tjmoore1993




PostPosted: Mon Apr 27, 2009 8:06 pm   Post subject: Re: RE:[Help] Get Statements

I will work on it a bit more. That was just a simple fix Smile
Euphoracle




PostPosted: Mon Apr 27, 2009 10:08 pm   Post subject: RE:[Help] Get Statements

Problem now is that people can't back-space errors, right?
Tony




PostPosted: Mon Apr 27, 2009 10:23 pm   Post subject: RE:[Help] Get Statements

and that one would have to type out a bunch of spaces, to enter a word less than 8 characters.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dusk Eagle




PostPosted: Mon Apr 27, 2009 10:31 pm   Post subject: Re: [Help] Get Statements

How about this Tony?
Turing:

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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Apr 27, 2009 10:54 pm   Post subject: RE:[Help] Get Statements

Crashes the program if you try to backspace on an empty string.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dusk Eagle




PostPosted: Mon Apr 27, 2009 11:01 pm   Post subject: Re: [Help] Get Statements

Well that's not hard to fix:
Turing:
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




PostPosted: Mon Apr 27, 2009 11:39 pm   Post subject: RE:[Help] Get Statements

I think that takes care of the backspace. Smile
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
tjmoore1993




PostPosted: Tue Apr 28, 2009 2:20 pm   Post subject: Re: [Help] Get Statements

Dusk Eagle @ Mon Apr 27, 2009 11:01 pm wrote:
Well that's not hard to fix:
Turing:
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!
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  [ 12 Posts ]
Jump to:   


Style:  
Search: