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

Username:   Password: 
 RegisterRegister   
 Get Value Certain Length
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
l0tt0




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




PostPosted: Thu Oct 23, 2003 5:09 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
PaddyLong




PostPosted: Thu Oct 23, 2003 8:42 pm   Post subject: (No 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
hackman




PostPosted: Mon Oct 27, 2003 11:45 am   Post subject: (No subject)

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




PostPosted: Mon Oct 27, 2003 6:11 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
hackman




PostPosted: Tue Oct 28, 2003 11:46 am   Post subject: (No subject)

i meant in the getch comand, as in

var ans:string (1)
getch (ans)
Tony




PostPosted: Tue Oct 28, 2003 5:33 pm   Post subject: (No subject)

ohhh Rolling Eyes lol Laughing

ch in "getch" is short for character.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
hackman




PostPosted: Wed Oct 29, 2003 10:51 am   Post subject: (No subject)

Very Happy wohoo my guese was right!
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  [ 8 Posts ]
Jump to:   


Style:  
Search: