# When I try to get my program to do something with a keypress it quits

Here's the code I have that's been giving me the problem:

PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
5 DO
IF INKEY\$ = CHR\$(128) + CHR\$(2) THEN CALL solo
IF INKEY\$ = CHR\$(128) + CHR\$(3) OR INKEY\$ = CHR\$(128) + CHR\$(4) THEN
PRINT "Sorry, cannot do that"
PLAY "01L2f"
GOTO 5
END IF
IF INKEY\$ = CHR\$(128) + CHR\$(5) THEN CALL ending
LOOP WHILE INKEY\$ = ""
END SUB

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."

I imcluded the first several lines so that you won't get an error message from it.

## Comments

• [code]
PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
DO
q\$ = INKEY\$
IF q\$ = "1" THEN
CALL solo
EXIT DO
ELSEIF q\$ = "2" OR q\$ = "3" THEN
PRINT "Sorry, cannot do that"
'PLAY "01L2f"
ELSEIF q\$ = "4" THEN
'CALL ending
EXIT DO
END IF
LOOP

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."
[/code]
• Before I say anything, I must know... which compiler are you using?

Davi Medrade
Master Programmer of Agnetron Software Ltda.
[email protected]
So Paulo, Brazil

• : Before I say anything, I must know... which compiler are you using?
:
:
: Davi Medrade
: Master Programmer of Agnetron Software Ltda.
: [email protected]
: So Paulo, Brazil
:
:
I am using QB 4.5
• : [code]
: PRINT ""
: PRINT "1. Play solo"
: PRINT "2. Play with one friend (coming soon)"
: PRINT "3. Play with two friends (also coming soon)"
: PRINT "4. Get outta here"
: DO
: q\$ = INKEY\$
: IF q\$ = "1" THEN
: CALL solo
: EXIT DO
: ELSEIF q\$ = "2" OR q\$ = "3" THEN
: PRINT "Sorry, cannot do that"
: 'PLAY "01L2f"
: ELSEIF q\$ = "4" THEN
: 'CALL ending
: EXIT DO
: END IF
: LOOP
:
: SUB solo
: CLS
: RANDOMIZE TIMER
: a = INT((12 - 6 + 1) + RND * 6)
: PRINT "Right now you hope to get the"
: PRINT "most money out of your given spins."
: [/code]
:
Ummm... I am using QB 4.5
[hr]
Jeffrey Hope

Jeffrey's Hermit Cave

• : Ummm... I am using QB 4.5

yeah...so what? what about my example doesnt work with qb4.5? ive never really used qb 4.5 much, but i designed it in qbx, but i programmed for years in qb1...so it should definitely work in qb4.5... i didnt change anything that would cause problems with compiler differences...and as far as the code we are talking about is concerned there is NO difference in qbx and qb4.5

i did forget to remove the REM (') statements i had put there because i didnt have the necessary sub "ending" and the play statement also raised an error...i didnt even look to see why it didnt work, you might need to change it..

[code]
PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
DO
q\$ = INKEY\$
IF q\$ = "1" THEN
CALL solo
EXIT DO
ELSEIF q\$ = "2" OR q\$ = "3" THEN
PRINT "Sorry, cannot do that"
PLAY "01L2f"
ELSEIF q\$ = "4" THEN
CALL ending
EXIT DO
END IF
LOOP

SUB solo
CLS
RANDOMIZE TIMER
a = INT((12 - 6 + 1) + RND * 6)
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."
[/code]
• I think that if you use labels instead of Line numbers, your progrram will be easier to understand.

Instead of:
[code]
1 PRINT "TEST"
[/code]
Use:
[code]
IAmALabel:
PRINT "TEST"
[/code]

Then, in the GOTO you can use the label instead of the line number:
[code]
GOTO IAmALabel
[/code]

Davi Medrade
Master Programmer of Agnetron Software Ltda.
[email protected]
So Paulo, Brazil

• [code]
PRINT ""
PRINT "1. Play solo"
PRINT "2. Play with one friend (coming soon)"
PRINT "3. Play with two friends (also coming soon)"
PRINT "4. Get outta here"
KeyLoop: 'This label will make your code easier to read
DO
[/code]
In the following lines, use the string with the number, instead of the codes, and put the key into a variable, because INKEY\$ is cleared each time you use it, then, the first IF will be true, if you pressed 2, but you used INKEY\$ in this if, then it will be cleared and the other IFs will be always false:
[code]
K\$=INKEY\$ 'Saves the current key
IF K\$ = "2" THEN CALL solo
IF K\$ = "3" OR K\$ = "4" THEN
PRINT "Sorry, cannot do that"
PLAY "01L2f"
GOTO 5
END IF
IF K\$ = "5" THEN CALL ending
LOOP
END 'Why END SUB here?

SUB solo
CLS
RANDOMIZE TIMER
a = INT(7 + RND * 6) 'Use the result, insted of the operation with constants
PRINT "Right now you hope to get the"
PRINT "most money out of your given spins."
END SUB 'You must use END SUB only here!

[/code]

I hope it will function.

And, remember: [blue]Indent your code. It will be easier to read and understand.[/blue]

Mail Me!
Davi Medrade
Master Programmer of Agnetron Software Ltda.
[email protected]
So Paulo, Brazil

• : : Ummm... I am using QB 4.5
:
: yeah...so what? what about my example doesnt work with qb4.5? ive never really used qb 4.5 much, but i designed it in qbx, but i programmed for years in qb1...so it should definitely work in qb4.5... i didnt change anything that would cause problems with compiler differences...and as far as the code we are talking about is concerned there is NO difference in qbx and qb4.5
:
: i did forget to remove the REM (') statements i had put there because i didnt have the necessary sub "ending" and the play statement also raised an error...i didnt even look to see why it didnt work, you might need to change it..
:
: [code]
: PRINT ""
: PRINT "1. Play solo"
: PRINT "2. Play with one friend (coming soon)"
: PRINT "3. Play with two friends (also coming soon)"
: PRINT "4. Get outta here"
: DO
: q\$ = INKEY\$
: IF q\$ = "1" THEN
: CALL solo
: EXIT DO
: ELSEIF q\$ = "2" OR q\$ = "3" THEN
: PRINT "Sorry, cannot do that"
: PLAY "01L2f"
: ELSEIF q\$ = "4" THEN
: CALL ending
: EXIT DO
: END IF
: LOOP
:
: SUB solo
: CLS
: RANDOMIZE TIMER
: a = INT((12 - 6 + 1) + RND * 6)
: PRINT "Right now you hope to get the"
: PRINT "most money out of your given spins."
: [/code]
:
Here's the ending sub:
[code]
SUB ending
FOR a = 1 TO 10
PRINT ""
NEXT a
PRINT "See the credits before you go (Y/N)?"
DO
[/code]
• : Ummm... I am using QB 4.5

Which I just tried and it works in flawlessly...
Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!