Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / messed up code

Author
Message
Blalack77
21
Years of Service
User Offline
Joined: 21st May 2005
Location:
Posted: 6th May 2008 03:25 Edited at: 6th May 2008 11:28
I'm just goofing around trying to make a cheesy little text based game and i'm running into an odd problem. I want to be able to have options and if you type "a" something happens, if you type "b" something happens, etc...But what happens is the same thing happens when you press a, b or c...heres my code so far:




Can someone help me, and if you see any shortcuts thus far please tell me too.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 6th May 2008 08:45
First off, I am assuming that you:
1. Don't indent your code, as that would have made it easier to find the problem. For instance:
This

It would look like this

2. Came from another language of BASIC recently (or other languages I guess, not too familiar with others)

Here is the problem:

You are using If-THEN's. In DBC, using then shows that that if statement is only 1 line long and it should do only what is after the then (thus the endif command isn't necessary).

So your logic is basically whatever they press, it will clear the screen then continue on with the program.

To fix it, change this:

To:


You would have to do it with ans1$="b" too but it is the same procedure.

Other things I noticed:
gosub start: ------You don't need the :, only when you place the label itself

if restart$="" -----What if the player types something? A better way would be to say press enter, then just use a wait key instead

if ans1$="a" ------DBC IS case sensitive. A<>a so if the player types A, nothing would happen. To fix, use upper$() or lower$(), it really doesn't matter which you use.

input "Press <Enter> to restart";restart$ ------If comfortable with subroutines, you could just make this a subroutine since it is the same thing for both cases, then just put a gosub instead of the same 2 lines of code.


Big shortcut, though slightly more complex and you shouldn't do it if you aren't sure how to use it (it will just make it worse...trust me) is the select-endselect command. It will change this:


To this:


So yeah, those are a few suggestions, you don't have to do any, just wanted to point them out as possibilities if you are feeling adventurous. For more info on select, it is in the main help under BASIC

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th May 2008 11:39
To add to what BN2 Productions has already said:

1. Use code boxes when you post code snippets. Simply highlight it and click on the Code button. I edited your post and did it for you this time...

2. Your program has a serious flaw - you are using Gosub which calls the procedure it's already in (recursively), yet you haven't used a Return at the end of it.

This will eventually fill up the stack and your program will die so you should never, ever do this.

You need to read a few tutorials which cover program layout and how procedures work. You'll find beginners tutorials 1 to 4 in my tutorials thread which is stickied at the top of this board.

TDK_Man

Login to post a reply

Server time is: 2026-07-12 18:00:11
Your offset time is: 2026-07-12 18:00:11