Computer Science Canada

problems with getch

Author:  hey_joe42 [ Thu Mar 13, 2003 4:47 pm ]
Post subject:  problems with getch

i have a problem with getch it only seems to work with strings of length 1
well what if i wanted to getch something at a certian length like four, like i wanted the user to type like 4 digits
like okay here is what the user in their heads are going to type
k but the variable can only hold 4 characters, so when the user gets to d of its input it stops them and they don't have to hit enter
how can you do this

Author:  Dan [ Thu Mar 13, 2003 4:59 pm ]
Post subject: 


var name:string (4)

put "put in 4 letters"
get name:4
put name

this will input as may letters as they type but will only store 4 of them in the varible name (the 1st 4).

Author:  Tony [ Thu Mar 13, 2003 5:11 pm ]
Post subject: 

it would be the same as

get text
put text(1..4)

the question as I can see is getting 4 characters without pressing enter. The answer - forloop!


var text:string := ""
var c:string(1)

for i:1..4
put c..
text += c
end for
put ""
put text

Author:  Asok [ Thu Mar 13, 2003 9:14 pm ]
Post subject: 

there is another way to do it that I am currently using for highscores (only 3 character initials)

var pnamec1 : string (1)
var pnamec2 : string (1)
var pnamec3 : string (1)
var pname : string (3)

put "Please enter player name:"..
getch (pnamec1)
put pnamec1 ..
getch (pnamec2)
put pnamec2 ..
getch (pnamec3)
put pnamec3
pname := pnamec1 + pnamec2 + pnamec3

It's sorta inefficient but if it's only for 4 characters who cares Wink

Author:  Tony [ Thu Mar 13, 2003 10:30 pm ]
Post subject: 

you see... the forloop does the same thing, but you can change how many characters you want Wink

thx for sharing your ideas though

Author:  hey_joe42 [ Fri Mar 14, 2003 12:53 pm ]
Post subject:  k here another one

now whatever well i was working on my program
i was switching everything to this cuz it just easy it not the sasme but there are no errors except i ran into this problem trying to change get product:* to get product:10 but what do i do if it two words?

Author:  azndragon [ Fri Mar 14, 2003 12:55 pm ]
Post subject: 

get product:10 will only accept the first 10 letters entered, or more accurately, only 10 ASCII values, which include spaces. If you want it to correctly accept input for 2 words, you have to type in 2 words, 1 with 5 letters, the other with 4 letters.

5 letters + 1 space + 4 letters = 10 letters total collected by Turing.

Author:  Tony [ Fri Mar 14, 2003 1:00 pm ]
Post subject: 

you can still employ a getch inside a forloop set to 10. It will input space as a character.