Computer Science Canada

"enter key" problem !!

Author:  ddndd [ Mon Dec 21, 2009 7:54 pm ]
Post subject:  "enter key" problem !!

What is it you are trying to achieve?
ok so im trying to make a program that asks the user for their names but it is a little diffrent b/c im going to use it for my tictactoe games

What is the problem you are having?
when ever the user inputs an enter the it will just go to the next line, im trying to stop that, i wanna make it ask for the name again but it is not working for some reason

Describe what you have tried to solve this problem
i tried diffrent things but nothing worked, if u guys have any ideas please tell me !!


Turing:

setscreen ("graphics:440;500,position:400;100,title: - = T I C T A C T O E =,nobuttonbar")
var name1 :string
var f1 : int := Font.New ("arial:20")
var f2 : int := Font.New ("arial:10")
var font : int := Font.New ("Goudy Stout:30")
drawfillbox(0,0,maxx,maxy,black)
loop
delay (10)
cls
drawfillbox(0,0,maxx,maxy,black)
drawfillbox (maxx div 2+200, maxy div 2 +15, maxx div 2 - 200, maxy div 2 - 15, white)
Draw.Text ("TicTacToe", maxx div 2 - 210, maxy -50 ,font,red)
Draw.Text ("Enter your name please",maxx div 2 - 150,maxy div 2 + 50,f1,red)
Text.Locate (16,4)
get name1
if name1 < "A" or name1 < "a" or name1 > "z" or name1 > "z" then
Draw.Text ("Error, Enter your name again", maxx div 2 - 100, maxy div 2 -50,f2,red)
get name1
end if
/*if name1 > "A" or name1 >"a" or name1 < "z" or name1 < "z" then
exit
end if*/

end loop



Please specify what version of Turing you are using
4.1

Author:  Zren [ Mon Dec 21, 2009 8:21 pm ]
Post subject:  RE:"enter key" problem !!

You kind of got it backwards a little. Right now it asks for your name again and again until you enter Z or A and then asks you again inside the loop. Instead it would be better to write the "Enter your name please" text every loop except the first. And exit the loop when the name is good.

Author:  Zasalamel [ Mon Dec 21, 2009 8:39 pm ]
Post subject:  RE:"enter key" problem !!

I do not know how to fix it easier then this:
Turing:

var name1 : char
put "Enter your name then press the shift key to continue"
loop
    exit when eof
    get name1
    exit when name1 = KEY_SHIFT
end loop

Author:  pjohnston [ Mon Dec 21, 2009 8:39 pm ]
Post subject:  Re: "enter key" problem !!

To prevent the input cursor from going to the next line, you have to erase the contents of the input box, and set the input cursor's location back to the beginning of the input box. See the following code for an example:


Turing:
if name1 < "A" or name1 < "a" or name1 > "z" or name1 > "z" then
    Draw.Text ("Error, Enter your name again", maxx div 2 - 100, maxy div 2 - 50, f2, red)
    Text.Locate (16, 4)
    for i : 1 .. 49
        put " " ..
    end for
    Text.Locate (16, 4)
    get name1
end if



Your if statement looks a bit funny by the way. Are you trying to check the string for non-letters? If so, you have to check every letter one at a time. Comparing the whole string won't work.

Author:  ddndd [ Mon Dec 21, 2009 9:25 pm ]
Post subject:  Re: RE:"enter key" problem !!

Zren @ Mon Dec 21, 2009 8:21 pm wrote:
You kind of got it backwards a little. Right now it asks for your name again and again until you enter Z or A and then asks you again inside the loop. Instead it would be better to write the "Enter your name please" text every loop except the first. And exit the loop when the name is good.

nope it is asking the user for their names the > a and < z is just to check if the user enters letters not other characters for ex numbers, and what do you mean the name is good ?? how im going to know if it is good or not ??

Author:  ddndd [ Mon Dec 21, 2009 9:27 pm ]
Post subject:  Re: RE:"enter key" problem !!

Zasalamel @ Mon Dec 21, 2009 8:39 pm wrote:
I do not know how to fix it easier then this:
Turing:

var name1 : char
put "Enter your name then press the shift key to continue"
loop
    exit when eof
    get name1
    exit when name1 = KEY_SHIFT
end loop


ok i did what u wrote and that didnt help much
the user still can press enter and it will go to the next line
and can u plz explain what u wrote in more details cuz i dont know how to use the char stuff since im only in grade 10 Razz

Author:  ddndd [ Mon Dec 21, 2009 9:29 pm ]
Post subject:  Re: "enter key" problem !!

pjohnston @ Mon Dec 21, 2009 8:39 pm wrote:
To prevent the input cursor from going to the next line, you have to erase the contents of the input box, and set the input cursor's location back to the beginning of the input box. See the following code for an example:


Turing:
if name1 < "A" or name1 < "a" or name1 > "z" or name1 > "z" then
    Draw.Text ("Error, Enter your name again", maxx div 2 - 100, maxy div 2 - 50, f2, red)
    Text.Locate (16, 4)
    for i : 1 .. 49
        put " " ..
    end for
    Text.Locate (16, 4)
    get name1
end if



Your if statement looks a bit funny by the way. Are you trying to check the string for non-letters? If so, you have to check every letter one at a time. Comparing the whole string won't work.


i tired ur code and still didnt really work, the user can still press enter and it will go to the next line, and for the if statments what is funny about it ??, u dont have to check every letter this way is much easier and simpler and does the exact same thing so ...

Author:  pjohnston [ Mon Dec 21, 2009 9:40 pm ]
Post subject:  Re: "enter key" problem !!

ddndd @ Mon Dec 21, 2009 9:29 pm wrote:

i tired ur code and still didnt really work, the user can still press enter and it will go to the next line, and for the if statments what is funny about it ??, u dont have to check every letter this way is much easier and simpler and does the exact same thing so ...


Did you delete your IF block and replace it with the one I suggested? When I tried that in Turing 4.1, it prevented the input cursor from moving to the next line in every case. Also, the comparison you are doing "if name1 < "A"......" will not catch invalid characters unless they are at the beginning of the string. Using comparison operators on strings only tells you which one comes first in alphabetical order.

Author:  ddndd [ Mon Dec 21, 2009 9:52 pm ]
Post subject:  Re: "enter key" problem !!

Did you delete your IF block and replace it with the one I suggested? When I tried that in Turing 4.1, it prevented the input cursor from moving to the next line in every case. Also, the comparison you are doing "if name1 < "A"......" will not catch invalid characters unless they are at the beginning of the string. Using comparison operators on strings only tells you which one comes first in alphabetical order.[/quote]

yes i did this is what the whole thing looks like and it still doesnt work !!

setscreen ("graphics:440;500,position:400;100,title: - = T I C T A C T O E =,nobuttonbar")
var name1 :string
var f1 : int := Font.New ("arial:20")
var f2 : int := Font.New ("arial:10")
var font : int := Font.New ("Goudy Stout:30")
const l: int := 16
const o: int := 4
drawfillbox(0,0,maxx,maxy,black)
loop
delay (10)
cls
drawfillbox(0,0,maxx,maxy,black)
drawfillbox (maxx div 2+200, maxy div 2 +15, maxx div 2 - 200, maxy div 2 - 15, white)
Draw.Text ("TicTacToe", maxx div 2 - 210, maxy -50 ,font,red)
Draw.Text ("Enter your name please",maxx div 2 - 150,maxy div 2 + 50,f1,red)
Text.Locate (16,4)
get name1
if name1 < "A" or name1 < "a" or name1 > "z" or name1 > "z" then
Draw.Text ("Error, Enter your name again", maxx div 2 - 100, maxy div 2 - 50, f2, red)
Text.Locate (16, 4)
for i : 1 .. 49
put " " ..
end for
Text.Locate (16, 4)
get name1
end if
if name1 > "A" or name1 >"a" or name1 < "z" or name1 < "z" then
exit
end if
end loop



and for the <a thing i know that but i used this so the user cant enter 123131 as their name but they can still do this u2323 which might be their nickname or something

Author:  Zren [ Mon Dec 21, 2009 9:57 pm ]
Post subject:  Re: RE:"enter key" problem !!

ddndd @ Mon Dec 21, 2009 9:25 pm wrote:
Zren @ Mon Dec 21, 2009 8:21 pm wrote:
You kind of got it backwards a little. Right now it asks for your name again and again until you enter Z or A and then asks you again inside the loop. Instead it would be better to write the "Enter your name please" text every loop except the first. And exit the loop when the name is good.

nope it is asking the user for their names the > a and < z is just to check if the user enters letters not other characters for ex numbers, and what do you mean the name is good ?? how im going to know if it is good or not ??


You program runs like this:
code:
loop
    text locate
    get name
    if name has non alphabetical characters then
        %Needs a text locate here in order to have it appear over the other text
        %To solve your origional problem
        %However this is not the right way to do it
        get name
    end if
end loop

You are asking for the name twice in the same loop. And at that, your origional text would still show without drawing over it first. Why? Why not just let the loop cycle and ask for the name again?

code:
loop
    text locate
    get name
    exit when name is okay
end loop


Or more specifically:
code:
loop
    text locate
    get name
    if name has only alphabetical characters then
        exit
    end if
end loop


Also. You need to look up the function ord since it will get a ASCII number value of a character. Then just look at your friend neighbourhood ASCII chart to look at the ranges you want.

Author:  ddndd [ Mon Dec 21, 2009 10:15 pm ]
Post subject:  Re: RE:"enter key" problem !!

Zren @ Mon Dec 21, 2009 9:57 pm wrote:
ddndd @ Mon Dec 21, 2009 9:25 pm wrote:
Zren @ Mon Dec 21, 2009 8:21 pm wrote:
You kind of got it backwards a little. Right now it asks for your name again and again until you enter Z or A and then asks you again inside the loop. Instead it would be better to write the "Enter your name please" text every loop except the first. And exit the loop when the name is good.

nope it is asking the user for their names the > a and < z is just to check if the user enters letters not other characters for ex numbers, and what do you mean the name is good ?? how im going to know if it is good or not ??


You program runs like this:
code:
loop
    text locate
    get name
    if name has non alphabetical characters then
        %Needs a text locate here in order to have it appear over the other text
        %To solve your origional problem
        %However this is not the right way to do it
        get name
    end if
end loop

You are asking for the name twice in the same loop. And at that, your origional text would still show without drawing over it first. Why? Why not just let the loop cycle and ask for the name again?

code:
loop
    text locate
    get name
    exit when name is okay
end loop


Or more specifically:
code:
loop
    text locate
    get name
    if name has only alphabetical characters then
        exit
    end if
end loop


Also. You need to look up the function ord since it will get a ASCII number value of a character. Then just look at your friend neighbourhood ASCII chart to look at the ranges you want.


ok so i did what u said fr the first part it is shorter now and kinda simpler, but i dont get the ord thing cuz it is too advanced fr me and ihavnt learned any thing about it in class so im not sure where to go with it i tried reading it and then looked up the ascii chart and i just didnt know what do next Sad
thanks for ur help tho Smile

Author:  Zren [ Mon Dec 21, 2009 10:25 pm ]
Post subject:  RE:"enter key" problem !!

Here's a copy of an ascii chart: http://www.jimprice.com/ascii-0-127.gif

put ord("A") %outputs 65

You can see that the ascii value of A is 65 while the lowercase is 97. Z=90 and z=122.

The diference between capitals and lowercase is 32.

You do know how to specify a range of numbers right?

Author:  ddndd [ Mon Dec 21, 2009 11:29 pm ]
Post subject:  Re: RE:"enter key" problem !!

Zren @ Mon Dec 21, 2009 10:25 pm wrote:
Here's a copy of an ascii chart: http://www.jimprice.com/ascii-0-127.gif

put ord("A") %outputs 65

You can see that the ascii value of A is 65 while the lowercase is 97. Z=90 and z=122.

The diference between capitals and lowercase is 32.

You do know how to specify a range of numbers right?


ummm do u mean using ifs ??
ex if x> >= 3 and x <=10 then ...
or something else ??
and btw i couldnt find the enter key on that chart

Author:  Zren [ Mon Dec 21, 2009 11:42 pm ]
Post subject:  RE:"enter key" problem !!

Why would you need the ASCII value of the enter key? Your checking to make sure each character in the string name is okay.
get name
Won't include the return at the end.

And yes. We can specify a range of numbers using two boolean statements.

if 10 > a and a < 15 then % 11, 12, 13, 14
if 10 >= a and a < 15 then % 10, 11, 12, 13, 14
if 10 > a and a <= 15 then % 11, 12, 13, 14, 15
if 10 > a and a < 15 then % 10, 11, 12, 13, 14, 15

Author:  ddndd [ Mon Dec 21, 2009 11:53 pm ]
Post subject:  Re: "enter key" problem !!

but why do i need to do that i dont want the user to be forced to put only letters u kno maybe someone wants to use m332 or something like that as their name i just want turing to know when they press the key enter it should just ask for the name again and not go to the next line

Author:  TheGuardian001 [ Mon Dec 21, 2009 11:56 pm ]
Post subject:  Re: "enter key" problem !!

To compare the individual letters of a string, you use the index of the character you want. So if you want to check the first letter:
code:

var name : string
get name
put name(1)

Author:  Zren [ Tue Dec 22, 2009 12:02 am ]
Post subject:  RE:"enter key" problem !!

Okay. But you want it to ask for the name again if there is numbers right? and if it's only letters then you exit the loop and finish the program right?

Author:  ddndd [ Tue Dec 22, 2009 12:16 am ]
Post subject:  Re: RE:"enter key" problem !!

Zren @ Tue Dec 22, 2009 12:02 am wrote:
Okay. But you want it to ask for the name again if there is numbers right? and if it's only letters then you exit the loop and finish the program right?


that is exactly what i want and i kinda did that but now im trying to make turing understand that when the user hits enter it shouldnt go to the next line it should just stay there and ask for the name again

Author:  ddndd [ Tue Dec 22, 2009 12:21 am ]
Post subject:  Re: "enter key" problem !!

TheGuardian001 @ Mon Dec 21, 2009 11:56 pm wrote:
To compare the individual letters of a string, you use the index of the character you want. So if you want to check the first letter:
code:

var name : string
get name
put name(1)


ok i didnt know that but that is not what im trying to do tho im trying to make turing not go to the next line when the user hits enter
thnx tho Smile

Author:  TheGuardian001 [ Tue Dec 22, 2009 12:30 am ]
Post subject:  Re: "enter key" problem !!

So, wait, if I understand this right, you want it to ask for their name twice on the same line?

If that's the case, I'd like you to go back to the start of the thread when the people told you to use locate(row,column).

Author:  ddndd [ Tue Dec 22, 2009 12:46 am ]
Post subject:  Re: "enter key" problem !!

TheGuardian001 @ Tue Dec 22, 2009 12:30 am wrote:
So, wait, if I understand this right, you want it to ask for their name twice on the same line?

If that's the case, I'd like you to go back to the start of the thread when the people told you to use locate(row,column).


ok if u run this

setscreen ("graphics:440;500,position:400;100,title: - = T I C T A C T O E =,nobuttonbar")
var name1 :string
var f1 : int := Font.New ("arial:20")
var f2 : int := Font.New ("arial:10")
var font : int := Font.New ("Goudy Stout:30")
const l: int := 16
const o: int := 4
drawfillbox(0,0,maxx,maxy,black)
loop
delay (10)
cls
drawfillbox(0,0,maxx,maxy,black)
drawfillbox (maxx div 2+200, maxy div 2 +15, maxx div 2 - 200, maxy div 2 - 15, white)
Draw.Text ("TicTacToe", maxx div 2 - 210, maxy -50 ,font,red)
Draw.Text ("Enter your name please",maxx div 2 - 150,maxy div 2 + 50,f1,red)
Text.Locate (16,4)
get name1
Text.Locate (l,o)
if name1 > "A" and name1 < "z" or name1 >"a" and name1 < "z" then
exit
end if
end loop

this is what is going to happen when the user hits enter instead of writing their name
Posted Image, might have been reduced in size. Click Image to view fullscreen.

see that white thing that is what happens when they press enter, i dont want turing to do that i want to just not skip the line and to just keep flashing inside of the white box
do u know what i mean know ??

Author:  TheGuardian001 [ Tue Dec 22, 2009 3:43 pm ]
Post subject:  Re: "enter key" problem !!

Yes, you mean you want it to keep it on the same line.
which means you want locate(row,column)
For example:
code:

var name : string
get name
locate (1, length(name)+2)
get name


locate will move the text cursor to the selected row and column.

Author:  Zren [ Tue Dec 22, 2009 4:58 pm ]
Post subject:  RE:"enter key" problem !!

Ooooooooh. What you want can't be done using get command. However it won't add the return value into the variable dude. So don't worry.

If you want to make it so you can only write Alphabetical characters and ignore numbers and return statements. You'll have to imitate the get command with getch(). I'll explain more so if your interested.

Also. Your if statement to check each character for numbers still doesn't work as intended. Your comparing a string to a character. You need to check each character in the string.

Author:  ddndd [ Tue Dec 22, 2009 6:08 pm ]
Post subject:  Re: "enter key" problem !!

TheGuardian001 @ Tue Dec 22, 2009 3:43 pm wrote:
Yes, you mean you want it to keep it on the same line.
which means you want locate(row,column)
For example:
code:

var name : string
get name
locate (1, length(name)+2)
get name


locate will move the text cursor to the selected row and column.


nope cuz when i hit enter it will just go to the next line and that is not what i want it to do !!

Author:  ddndd [ Tue Dec 22, 2009 6:11 pm ]
Post subject:  Re: RE:"enter key" problem !!

Zren @ Tue Dec 22, 2009 4:58 pm wrote:
Ooooooooh. What you want can't be done using get command. However it won't add the return value into the variable dude. So don't worry.

If you want to make it so you can only write Alphabetical characters and ignore numbers and return statements. You'll have to imitate the get command with getch(). I'll explain more so if your interested.

Also. Your if statement to check each character for numbers still doesn't work as intended. Your comparing a string to a character. You need to check each character in the string.


yeeeees :p
ok im interested i know some stuff about getch but i think if i used it the user will be able to just enter one character and that is it do you know what i mean??
fr ex:
var name : string (1)
getch (name)
get name

and if i put 5 instead of the 1 then it will just give me an error !!

Author:  Zren [ Tue Dec 22, 2009 6:26 pm ]
Post subject:  RE:"enter key" problem !!

code:

View.Set ("offscreenonly")
var ch : string (1)
var str : string := ""
loop
    if hasch () then
        getch (ch)
        str += ch
    end if
    cls
    put str
    View.Update
    delay (10)
end loop


This basically imitates get. It allows you to understand how you would gather each character one at a time and add it to a string. Lucky for us Backspace still works. And with a little modding you can check the character press to check if it's the Enter key or a number. Just don't forget to include Backspace if you limit to only letters.

Try getting to limit the numbers in a separate program first, then bring it over to your main program. It's a lot easier.

Author:  pjohnston [ Tue Dec 22, 2009 9:54 pm ]
Post subject:  Re: "enter key" problem !!

ddndd @ Mon Dec 21, 2009 11:53 pm wrote:
but why do i need to do that i dont want the user to be forced to put only letters u kno maybe someone wants to use m332 or something like that as their name i just want turing to know when they press the key enter it should just ask for the name again and not go to the next line


Yeah... I just got that. I wasn't sure exactly what the input checker was supposed to do. Sorry for the confusion.


: