help on limiting keys

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Nekrophidius wrote:
Z!re wrote:

Code: Select all

DO
k$ = INKEY$
LOOP UNTIL k$ = "w"
SELECT CASE k$
[...]
You bonehead...that specific code is never gonna work. :D k$ is ALWAYS going to be w after the loop because the loop won't end until it is so your SELECT CASE will only ever work with CASE "w". :P Silly Swede... ;)
Yes, just like all previous code snippets..
I have left this dump.
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

(PaulUnknown's "nevermine" notwithstanding...)

Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Zim wrote:(PaulUnknown's "nevermine" notwithstanding...)

Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.
Very sound advice, Zim. Hope it doesn't fall on deaf ears. What'd you say? :shock:
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

Just remember, if you're going to use SELECT CASE after a keyboard input, you'd better have a CASE ELSE at the end of your case list, or you'll have to have a CASE for every key on the keyboard! Otherwise, after the first keystroke you're not checking for, the program bombs.
Sorry but I don't know what you are saying.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

If you use a Select Case, and you don't use a CASE ELSE,
then you will end up doing this.

Code: Select all

SELECT CASE Cheese$
 CASE IS "A" 
  ...
 CASE IS "B"
  ...
 CASE IS "C"
  PRINT "TRY AGAIN"
 CASE IS "D"
  PRINT "TRY AGAIN"
 CASE IS "E"
  PRINT "TRY AGAIN"
 CASE IS "F"
  PRINT "TRY AGAIN"
...
 CASE IS "Z"
  PRINT "TRY AGAIN"
You get what I mean, right? Typing out all of the letters? Yeah, thats right... but instead you can do this:

Code: Select all

SELECT CASE Cheese$
 CASE IS "A"
  PRINT "YOU WIN!"
 CASE IS "B"
  PRINT "YOU WIN!"
 CASE ELSE
  PRINT "YOU FAIL!"
Yeah, you get it now? But if you command your CASEs to use subs/functions/GOTOs, then it will exit without doing anything which can REALLY screw your game/program/OS up...

*caugh* No, I didn't say just like in windows... *caugh*

I hope you got that...
Image
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

GoTo's can be dangerous, but subs and functions should be ok... no?
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

Well that's what I wanted to do.
If I limited the cases possible before SELECT CASE then I don't need CASE ELSE, right?
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Right!

But you have to be absolutely sure that (at least) ONE of the cases is true, else the program will bomb. I usually do something like this:

Code: Select all


Input "Option (1) or (2) ", k

Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else 'do nothing
End Select

Adding that one little line with a comment " 'do nothing " prevents the program from an abnormal abort. Very little code; good insurance; lots of bang for the buck.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Zim wrote:Right!

But you have to be absolutely sure that (at least) ONE of the cases is true, else the program will bomb. I usually do something like this:

Code: Select all


Input "Option (1) or (2) ", k

Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else 'do nothing
End Select

Adding that one little line with a comment " 'do nothing " prevents the program from an abnormal abort. Very little code; good insurance; lots of bang for the buck.
Good point, Zim. But if the user hits anything other than 1 or 2, he would get absolutely no output response and wonder ???.

I would enhance your code as follows:

Code: Select all

Input "Option (1) or (2) ", k
Select Case k
   case 1 : print "You said ONE"
   case 2 : print "You said TWO"
   case else : print "Error: Invalid Option"
End Select 
*****
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Even better!
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
Post Reply