Computer Science Canada

Get Value Certain Length

Author:  l0tt0 [ Thu Oct 23, 2003 4:52 pm ]
Post subject:  Get Value Certain Length

Is there anyway taht when you use
get variable
that it can only be a certain amount of characters?

Say I have a yes or no question i just want them to enter y or n...

anyone? thanks in advance

Author:  Tony [ Thu Oct 23, 2003 5:09 pm ]
Post subject: 

code:

var letter:string(1) %<- note the (1), its important
getch(letter)
put letter


getch() reads a single character from input buffer. If you want more then a single letter of input, you would run getch in a forloop.

Author:  PaddyLong [ Thu Oct 23, 2003 8:42 pm ]
Post subject: 

a function I just wrote up ... hope it does what you need

it will get characters for the string until enter is pressed or until there are the set number of characters. if you want it to keep going even if they push enter (so it will always get exactly the number of characters you specify) then take out the "ord (ch) = 10 or " in the exit statement

code:

function getWord (wordLength : int) : string
    var word, tempWord : string := ""
    var ch : string (1)

    loop
        getch (ch)
        if ord (ch) >= 32 and ord (ch) <= 126 then
            put ch ..
            word += ch
        elsif ord (ch) = 8 and length (word) >= 1 then % if it is backspace
            tempWord := word
            word := ""
            locate (whatrow, whatcol - 1)
            put " " ..
            locate (whatrow, whatcol - 1)
            for q : 1 .. length (tempWord) - 1
                word += tempWord (q)
            end for
        end if
        exit when ord (ch) = 10 or length (word) = wordLength
    end loop
    put ""
    result word
end getWord

var someString : string
someString := getWord (5)
put someString

Author:  hackman [ Mon Oct 27, 2003 11:45 am ]
Post subject: 

Not realy relavent but...what dose ch in getch stand for?

Author:  Tony [ Mon Oct 27, 2003 6:11 pm ]
Post subject: 

should have read the whole code first Rolling Eyes
code:

var ch : string (1)


ch is a name of a variable of string(1) type

Author:  hackman [ Tue Oct 28, 2003 11:46 am ]
Post subject: 

i meant in the getch comand, as in

var ans:string (1)
getch (ans)

Author:  Tony [ Tue Oct 28, 2003 5:33 pm ]
Post subject: 

ohhh Rolling Eyes lol Laughing

ch in "getch" is short for character.

Author:  hackman [ Wed Oct 29, 2003 10:51 am ]
Post subject: 

Very Happy wohoo my guese was right!


: