Qbasicnews.com
July 02, 2020, 12:03:32 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3
  Print  
Author Topic: SELECT CASE, or something  (Read 9151 times)
tabnewflax
New Member

Posts: 22


« on: March 10, 2004, 08:13:24 AM »

INPUT "1. Yes 2. No", choice
SELECT CASE choice
CASE 1
    GOTO yes
CASE 2
    GOTO no
END SELECT

That is just an example of my problem. So my problem is that you could just press ENTER or a digit except 1 and 2 to get to the next section in the game. Which kinda makes the game worthless.

How do I type so the only possible options is 1 and 2?
Logged
R@dioman
Ancient QBer
****
Posts: 410



« Reply #1 on: March 10, 2004, 08:18:00 AM »

You can use CASE ELSE like so:

Code:

Menu:
INPUT "1 or 2", choice

SELECT CASE
   CASE 1
       GOTO Yes
   CASE 2
       GOTO No
   CASE ELSE
       PRINT "Select either 1 or 2 please"
       GOTO Menu
END SELECT
Logged

Agamemnus
x/ \z
*****
Posts: 3491



« Reply #2 on: March 10, 2004, 01:47:26 PM »

This is just the proverbial tip of the iceberg.

You can change your keyboard input in many ways:

*use a different input command, like I$ = INKEY$ or n% = INP(96).
*error-check the answer by asking for a string, and then error-check it: get rid of leading/trailing spaces, etc.
*You can use key/mouse-selection menus so there are no invalid answers.

You can find some things here:
faq.qbasicnews.com
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Mango
Wandering Guru
***
Posts: 360



« Reply #3 on: March 11, 2004, 01:13:41 AM »

Quote from: "Agamemnus"
This is just the proverbial tip of the barrel.


Is it that or is it the proverbial tip of the iceberg??!!??   :-?  :Huh:
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #4 on: March 11, 2004, 01:40:48 AM »

What are you talking about?
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #5 on: March 11, 2004, 02:00:30 AM »

Quote
Last edited by Agamemnus on Wed Mar 10, 2004 11:39 pm; edited 1 time in total
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #6 on: March 11, 2004, 02:15:06 AM »

Ok, I admit it.

Yeah.

It's iceberg.
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Mac
Senior Member
**
Posts: 243


WWW
« Reply #7 on: March 14, 2004, 01:04:09 AM »

Mac
Logged
Sumo Jo
Been there, done that
*****
Posts: 1244



WWW
« Reply #8 on: March 14, 2004, 01:18:34 AM »

Quote from: "Mac"
Mac


spammer
Logged

url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
Pyrodap
Senior Member
**
Posts: 166



« Reply #9 on: March 14, 2004, 01:30:24 AM »

Try this...

PRINT "PRESS EITHER 1 OR 2"

DO
K$ = INKEY$
LOOP UNTIL K$ = "1" OR K$ = "2"

SELECT CASE K$
CASE "1"
'Code for 1
CASE "2"
'Code for 2
END SELECT
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #10 on: March 14, 2004, 01:31:42 AM »

So, tabnewflax, did Radioman's example solve your question, or do you further doubts?
*****
Logged
Blitz
I hold this place together
*****
Posts: 853



WWW
« Reply #11 on: March 14, 2004, 06:55:29 AM »

I think this is better coding practice.

Code:

do    
    input "Enter selection", ikey
   
    select case ikey
        case "1"
            print "You pressed 1"
            exit do
           
        case "2"
            print "You pressed 2"
            exit do
           
        case else
            print "Invalid option, try again"
    end select
loop
Logged

oship me and i will give you lots of guurrls and beeea
TheBigBasicQ
*/-\*
*****
Posts: 4550



WWW
« Reply #12 on: March 14, 2004, 11:07:34 AM »

Quote from: "sumojo"
Quote from: "Mac"
Mac


spammer


Read the subject =P

BTW You could try this if you have lot of code in each case:
Code:
do  
    input "Enter key ", ikey
   
    select case ikey
        case "1"
            CALL KEY1
            exit do
           
        case "2"
            CALL KEY2
            exit do
         case ...
         ...
        case else
            print "Invalid!!!"
    end select
loop
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #13 on: March 14, 2004, 01:45:32 PM »

This is MUCH more sane:
Code:

DO
INPUT "Enter the !#$!@#$@#$ key already!!", ikey
SELECT CASE ikey
CASE "1": key1: EXIT DO
CASE "2": key2: EXIT DO
CASE ......
CASE ELSE: PRINT "INVALID!"
END SELECT
LOOP
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #14 on: March 14, 2004, 09:50:51 PM »

Ah, yes, I like Aga's example. It reads so much better. Some people may not like multiple statements on one line, but they sure make the code look nice. You can see the whole thing with one glance.
*****
Logged
Pages: [1] 2 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!