Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: tabnewflax on March 10, 2004, 08:13:24 AM



Title: SELECT CASE, or something
Post by: tabnewflax 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?


Title: SELECT CASE, or something
Post by: R@dioman 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


Title: SELECT CASE, or something
Post by: Agamemnus 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


Title: SELECT CASE, or something
Post by: Mango 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??!!??   :-?  :???:


Title: SELECT CASE, or something
Post by: Agamemnus on March 11, 2004, 01:40:48 AM
What are you talking about?


Title: SELECT CASE, or something
Post by: Plasma 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


Title: SELECT CASE, or something
Post by: Agamemnus on March 11, 2004, 02:15:06 AM
Ok, I admit it.

Yeah.

It's iceberg.


Title: :rotfl:
Post by: Mac on March 14, 2004, 01:04:09 AM
Mac


Title: Re: :rotfl:
Post by: Sumo Jo on March 14, 2004, 01:18:34 AM
Quote from: "Mac"
Mac


spammer


Title: SELECT CASE, or something
Post by: Pyrodap 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


Title: SELECT CASE, or something
Post by: Moneo on March 14, 2004, 01:31:42 AM
So, tabnewflax, did Radioman's example solve your question, or do you further doubts?
*****


Title: SELECT CASE, or something
Post by: Blitz 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


Title: Re: :rotfl:
Post by: TheBigBasicQ 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


Title: SELECT CASE, or something
Post by: Agamemnus 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


Title: SELECT CASE, or something
Post by: Moneo 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.
*****


Title: SELECT CASE, or something
Post by: qbiscool on March 15, 2004, 07:27:23 PM
or if you realy want to you could use goto's

Quote

input "hit a key"; j$
if j$ = "w" then
print "you hit w"
else if j$ = "r" then print "you hit r"
else print "you hit "j$
end if



or
Quote

on error goto goterror
print "hit a key or esc to exit"
do
key$ = inkey$
do
loop until key$ <> ""
print key$
if key$ <> chr$(27) then loop else system
system

goterror:
print "found error"
do
print "[r]etry, gnore, [f]ail"
do
key$ = inkey$
if key$ = "r" then resume
if key$ = "i" then resume next
if key$ = "f" then system
loop until key$ <> ""
loop


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 16, 2004, 07:46:41 AM
/me thinks aga is insane =P

Qbiscool. goto statements are dangerous at the hands of a newbie =(. So I would rather advice him to use gosub.


Title: SELECT CASE, or something
Post by: Neo on March 16, 2004, 09:42:12 AM
Quote from: "TheBigBasicQ"
Qbiscool. goto statements are dangerous at the hands of a newbie =(. So I would rather advice him to use gosub.

So... you discriminate people on their knowledge and then say either 'YOU SHOULD USE SUBs' (experienced) or 'YOU SHOULD USE GOSUB' (newb)... ;)


Quote from: "Moneo"
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.

I'd really like it though if it had some identing... ;)


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 16, 2004, 09:45:19 AM
No, i wasnt discriminating =P. I was just saying that goto is dangerous. I dont like GOSUB(personally) so I suggested SUB =).


Title: SELECT CASE, or something
Post by: Neo on March 16, 2004, 09:48:02 AM
*Inproper* use of GOSUB: Out of stack space
GOTO: Spaghetti code (sometimes causing stack error as well)
SUBS: Optimal procedural programming.

I think I agree with you ;)

Btw, why, why, why does ON ERROR only support GOTO and not CALL SUB or so... :(


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 16, 2004, 09:49:48 AM
How how how should I know =P? But I dont find it inconvinient =P.


Title: SELECT CASE, or something
Post by: Agamemnus on March 16, 2004, 09:50:02 AM
Simple. My program modified to have tabs:

Code:

DO                                                      '7 tabs
INPUT "Enter the !#$!@#$@#$ key already!!", ikey        '1 tab
SELECT CASE ikey                                        '5 tabs
CASE "1": key1: EXIT DO                                 '5 tabs
CASE "2": key2: EXIT DO                                 '5 tabs
CASE ......
CASE ELSE: PRINT "INVALID!"                             '4 tabs
END SELECT                                              '6 tabs
LOOP                                                    '7 tabs


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 16, 2004, 09:51:15 AM
Quote from: "Agamemnus"
Simple. My program modified to have tabs:

Code:

DO                                                      '7 tabs
INPUT "Enter the !#$!@#$@#$ key already!!", ikey        '1 tab
SELECT CASE ikey                                        '5 tabs
CASE "1": key1: EXIT DO                                 '5 tabs
CASE "2": key2: EXIT DO                                 '5 tabs
CASE ......
CASE ELSE: PRINT "INVALID!"                             '4 tabs
END SELECT                                              '6 tabs
LOOP                                                    '7 tabs


I dont get the relevance?


Title: SELECT CASE, or something
Post by: Neo on March 16, 2004, 09:52:36 AM
Quote from: "Agamemnus"
Simple. My program modified to have tabs:

Hrmmm... where are the tabs?

I meant something like this:
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


Title: SELECT CASE, or something
Post by: Moneo on March 16, 2004, 11:49:08 PM
Neo, I have a feeling that Aga doesn't waste time indenting or formating sample code. I guesss he figures if you like it you'll adopt it and format it to your taste.
*****l


Title: SELECT CASE, or something
Post by: Agamemnus on March 17, 2004, 12:16:42 AM
tBBQ: in response to Neo.
In regards to GOSUBs, I think that they're usually not necessary, but they still have use. I think that there could have been a lot more done with QB4.5's GOSUB/SUB thing, such as multiple layers:

1) Commands in one SUB don't respond to labels in other SUBs, but the label namespace is global not local. (ie. only one "blah:" per program)

2) GOSUBs are a nice organizational tool when you have a lot of SUBs or very few SUBs. Could make the IDE a lot cooler if you could see GOSUBs in a SUB, and maybe SUBs in SuperSUBs. (ie modules)

Neo: After the code and before the comments.

Moneo: Yup, yup, very sensible...


Title: SELECT CASE, or something
Post by: qbiscool on March 17, 2004, 08:51:02 PM
hey for that ON ERROR GOTO goterror could i use two ON ERROR's
like this:

Quote

ON ERROR GOTO 1
bload "",d,3

on error goto 2
open "" for output as #1
close #1

1
resume next

2
system


Title: SELECT CASE, or something
Post by: whitetiger0990 on March 17, 2004, 11:33:22 PM
Like this?
Code:
CLS
ON ERROR GOTO 1
OPEN "aBaBaB1" FOR INPUT AS #1
ON ERROR GOTO 2
OPEN "aBaBaB2" FOR INPUT AS #1
CLOSE #1
END
1
PRINT 1
RESUME NEXT

2
PRINT 2
RESUME NEXT


hmm, guess so. it prints 1 and 2.
(and if you remove one of the OPENs it only prints one number so...)


Title: SELECT CASE, or something
Post by: Blitz on March 18, 2004, 09:12:41 AM
QBs error handling is the crappiest evarrr.


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 18, 2004, 12:11:03 PM
Nope you put only one ON ERROR statement. In order to check which error actually caused your error handler to be called you would use ERR command. Like this:

Code:
CLS
ON ERROR GOTO errhandler

CHDIR "e:"

END
errhandler:
IF ERR = 71 THEN
   PRINT "ErrorHandler: device not ready"
ELSEIF ERR = 76 THEN
   PRINT "ErrorHandler: Invalid drive specification"
END IF

RESUME NEXT


Change the drive letter to a non existing one and you will trap the second error while change it to "a:" and dont put any floppy into it and you will have it catch the first error =)


Title: SELECT CASE, or something
Post by: Blitz on March 18, 2004, 06:29:17 PM
Exactly, the crappiest evarrrrr.


Title: SELECT CASE, or something
Post by: Agamemnus on March 18, 2004, 07:04:23 PM
It's better than nothing.

What kind of error handling system do you suggest, anyways???


Title: SELECT CASE, or something
Post by: na_th_an on March 18, 2004, 08:48:19 PM
You can do the error handling yourself. Check stuff that may fail.


Title: SELECT CASE, or something
Post by: R@dioman on March 18, 2004, 10:17:42 PM
Nathan wrote:
Quote

You can do the error handling yourself. Check stuff that may fail.


Suppose you're initializing something, like your sound, or memory. Write it as a function that sends back 0 if everything went alright, or sends an error code explaining the error if something failed.


Title: SELECT CASE, or something
Post by: oracle on March 19, 2004, 12:44:52 AM
Which is exactly how it should be done, and is done in my project.

ps: for those of you who want to quote somebody without using the quote button:

Quote from: "name_of_person"
persons_text


Title: SELECT CASE, or something
Post by: Neo on March 22, 2004, 07:17:55 AM
Hehe. We made the prettiest Initialization routine ever... ;)


Title: SELECT CASE, or something
Post by: adosorken on March 22, 2004, 10:47:15 AM
Quote from: "Neo"
GOTO: Spaghetti code (sometimes causing stack error as well)

GOTOs boil down to a JMP and do not use stack space. I have no idea where you pulled this one from.  :???:

Quote from: "Neo"
Btw, why, why, why does ON ERROR only support GOTO and not CALL SUB or so... :(

My theory is that QB was developed at a time when companies were still being weaned off of line numbers for BASIC programming. :D


Title: SELECT CASE, or something
Post by: Neo on March 23, 2004, 07:22:49 AM
Quote from: "Nek"
GOTOs boil down to a JMP and do not use stack space. I have no idea where you pulled this one from.  :???:

Using a lot of GOTOs in your code makes your code spaghetti. You can't exactly see how the program runs when looking at the code. (try Aga's code... j/k  :rotfl: )


Title: SELECT CASE, or something
Post by: na_th_an on March 23, 2004, 10:35:12 AM
But it has nothing to do with the Stack. It may be ugly and unreadable, but it ain't unefficient or dangerous.


Title: SELECT CASE, or something
Post by: adosorken on March 23, 2004, 11:48:30 AM
On the contrary...being from a C64 BASIC and assembly background, spaghetti code to me is far more readable than procedural code because I can just scroll around spaghetti code but have to keep hitting the F2 key for procedural code and it's a pain in the ass to be honest. I can read Agamemnus' code just fine and I'd have to say he's one of the best coders here. Of course, not everyone has a background in writing efficient spaghetti code ;) so I guess this doesn't apply to some :D


Title: SELECT CASE, or something
Post by: na_th_an on March 23, 2004, 12:47:48 PM
I come from Spaghetti realms as well, I coded in my ZX Spectrum from 1988 to 1993. I can read Aga's code as well and I think he's one of the best coders here, as well. But I could change my mind and benefit from the advantages that procedural approaches have over line_numbers-GOTO'ed-GOSUB'ed-non_block_IF'ed-etc approaches :P

This is code from a ZX Spectrum game I was coding this summer: this moves the enemies :P

Code:
2000 REM selectGFX
2010 LET aa=PEEK (dir+6): GO SUB 930
2015 IF PEEK (dir+7)=0 THEN GO TO 2050: REM left
2025 IF PEEK (dir+4)=1 THEN GO TO 2040
2030 IF p THEN LET f$="./": GO TO 2032
2031 LET f$="01"
2032 LET e$=",-": RETURN
2040 IF p THEN LET f$=":;": GO TO 2042
2041 LET f$="<="
2042 LET e$="89": RETURN
2050 IF PEEK (dir+4)=1 THEN GO TO 2070
2060 IF p THEN LET f$="45": GO TO 2062
2061 LET f$="67"
2062 LET e$="23": RETURN
2070 IF p THEN LET f$="@A": GO TO 2072
2071 LET f$="BC"
2072 LET e$=">?": RETURN
2100 REM ENEMIGOS-MOVER
2110 FOR i=1  TO 2
2111 LET dir=benem+16*px+80*py+(i-1)*8
2120 GO SUB 2000: REM selectGFX
2132 LET old=PEEK (dir+6)
2140 POKE dir+6,PEEK (dir+6)+PEEK (dir+7)-1
2150 IF PEEK (dir+6)=PEEK (dir+1)  OR PEEK (dir+6)=PEEK (dir+2)  THEN POKE dir+7, 1-(PEEK (dir+7)-1)
2155 IF PEEK dir  THEN GO TO 2200
2157 REM HORIZONTAL
2160 IF old<PEEK (dir+6)  THEN PRINT PAPER ps;  INK is;AT PEEK (dir+3)-1,PEEK (dir+6)-1;"a"; INK PEEK (dir+5);e$;AT PEEK (dir+3),PEEK (dir+6)-1; INK is;"a"; INK PEEK (dir+5);f$: GO TO 2180
2170 PRINT PAPER ps;  INK PEEK (dir+5);AT PEEK (dir+3)-1,PEEK (dir+6);e$; INK is;"a"; INK PEEK (dir+5);AT PEEK (dir+3),PEEK (dir+6);f$; INK is;"a"
2180 IF PEEK (dir+6)>=a-1 AND PEEK (dir+6)<=a+1 AND PEEK (dir+3)>=w-1 AND PEEK (dir+3)<=w+1 THEN LET vi=vi-1: GO SUB 701
2181 GO TO 2300
2200 REM VERTICAL
2210 IF old<PEEK (dir+6)  THEN PRINT PAPER ps;AT PEEK (dir+6)-2,PEEK (dir+3); INK is;"aa"; INK PEEK (dir+5);AT PEEK (dir+6)-1,PEEK (dir+3);e$;AT PEEK (dir+6),PEEK (dir+3);f$: GO TO 2230
2220 PRINT PAPER ps;AT PEEK (dir+6)+1,PEEK (dir+3); INK is;"aa"; INK PEEK (dir+5);AT PEEK (dir+6)-1,PEEK (dir+3);e$;AT PEEK (dir+6),PEEK (dir+3);f$
2230 IF PEEK (dir+6)>=w-1 AND PEEK (dir+6)<=w+1 AND PEEK (dir+3)>=a-1 AND PEEK (dir+3)<=a+1 THEN LET vi=vi-1: GO SUB 701
2300 NEXT i
2301 RETURN


That's Spaghetti, kiddo :D

It's better to go procedural/structured 'cause it is easier to understand other languages, not just BASIC.


Title: SELECT CASE, or something
Post by: TheBigBasicQ on March 24, 2004, 05:02:02 AM
Nath that may be spagetti code for you but i've seen such code in most of the older books published around in the 70s and 80s. Just because we have more structured languages now doesnt mean that the code in the older languages is spagetti =).


Title: SELECT CASE, or something
Post by: Neo on March 24, 2004, 07:30:32 AM
Quote from: "na_th_an"
But it has nothing to do with the Stack. It may be ugly and unreadable, but it ain't unefficient or dangerous.
I wasn't talking about the stack or the ASM clone of the code :P It's just unreadable to humans but for the computer, in ASM it is all the same, some jmps to a specified address, both with loops (DO, FOR, WHILE) and GOTOs.


Title: SELECT CASE, or something
Post by: na_th_an on March 24, 2004, 10:55:53 AM
Quote from: "TheBigBasicQ"
Nath that may be spagetti code for you but i've seen such code in most of the older books published around in the 70s and 80s. Just because we have more structured languages now doesnt mean that the code in the older languages is spagetti =).


That depends on what you call spaghetti code. I call it "spaghetti code" when the code looks like a plate of spaghetti: You follow a "run path" and it goes to line 10, keeps on to line 110, then it GOTOs to line 20, then it GOTOs to line 2000, having GOSUBed to line 6500... Like a plate of spaghetti, where every spaghetto is twisted, tied, and goes around other spaghetti. That's the metaphor I think the therm is based on :P

Therefore ol' 8 bits dialects of BASIC are spaghetti makers :P

Neo: I was replying to this concrete phrase:

Quote from: "Neo"
GOTO: Spaghetti code (sometimes causing stack error as well)


You said that GOTO "sometimes causes stack error as well", but that will never happen as GOTOs doesn't touch the stack.