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.

Newcomers DBPro Corner / DBP - Need some help with my code (save & retrieve data)

Author
Message
Ozone88101
11
Years of Service
User Offline
Joined: 27th Nov 2012
Location:
Posted: 27th Nov 2012 21:05
I am trying to write some code that will accept various lines of information, save that information to a file and then recall that information. I get syntax error when I compile.

Any help would be appreciated as I havent done much programming since my high school days of using BasicA. thanks



There are 10 types of people in this world; Those that know Binary and those that don't.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Nov 2012 09:54
Oooook, where to begin. For starters, any gosub you define should have a RETURN statement, otherwise you'll just run into the next subroutine. And if you indent your code, you should be able to find your nesting error pretty easily.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 28th Nov 2012 19:09
As Phaelax stated, your nesting problem is because you didn't indent your code so you cannot keep track of it. You are missing an ENDIF to close your last IF-block.

But that's not the only problem. It seems you are using GOSUB in the wrong context (I noticed you went GOSUB at the last line). Your code suggests you are trying to use GOSUB as GOTO. Don't confuse these commands, they are very different. A subroutine and a label are different implementations of jumping.



This is prolly what you were trying to do:



Hopefully this clarifies things for you somewhat.

RP Functions Library v1.0

My signature has not been erased by a mod.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Nov 2012 20:45
If he just puts a return statement at the end of those segments the code will also work.

Considering where he said his coding experience is coming from, it does make more sense that he was trying to use GOTO. A habit we will gladly break for him!

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 29th Nov 2012 11:40
I think you are actually using Gosub as a loop branch statement. It returns to the main menu at the end of the procedure.

Check out TDKs tutorials first before continuing, at least for one hour; you'll save yourself lots of time in the long run. I'm not trying to say that the code is terrible, it's just that there are a number things you have done that are just waiting to cause you further problems; for example what happens if I miss type 1 and end up typing `. It will quit the program. Some of the lists there could have been automated; and the program could have been written in about 10 lines of code, you've made your task a little more difficult for yourself than it needs to be.

Hope all goes well.

Ozone88101
11
Years of Service
User Offline
Joined: 27th Nov 2012
Location:
Posted: 30th Nov 2012 00:18
I tried the above suggestions and it still errors on me at the bottom of the code.

There are 10 types of people in this world; Those that know Binary and those that don't.
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 30th Nov 2012 23:28
Post the code you have

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Dec 2012 11:25
Here is you code after patching:


It may work but your approach is wrong and your formatting needs a lot of rethinking. This is fine, we all try things the lazy way at some point but if you persevere, you'll get used to doing these things:
   ■ Indent your nest
   ■ Indent all code in your main module if you use labels
   ■ Functional programming is much better for complex code
   ■ Have a logical variable-naming system. Adapt with the language
   ■ Check your spelling on variables (DBP allows undeclared ones)
   ■ Only use GOTO for tiny loops or to enhance performance.
   ■ Try not to use GOSUB.
   ■ Understand how a command works.

Some things about DBP you need to know (I did Qbasic in the day)
   ■ Everything is $Dynamic in DBPro (that I can think of)
   ■ All arrays are global [by default?]
   ■ DBPro supports basic pointers.
   ■ DBPro does not require local variables to be declared

Good luck.

RP Functions Library v1.0

My signature has not been erased by a mod.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Dec 2012 12:03
Quote: "■ Try not to use GOSUB."


Why?

TheComet

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Dec 2012 17:58
@TheComet:
Why am I saying to avoid GOSUB? Well, tbh, it's mainly subjective reasoning but there are a couple of legitimate, objective reasons too.

   ■ Lack of scope change can wreak havoc with variables with the same name.
   ■ Lack of scope change doesn't delete temporary local variables.
   ■ Code debugging can be harder merely due to formatting of labels vs formatting of functions.
   ■ Labels don't accept parameters nor return values.
   ■ I think that in most cases labels look messy. It takes careful formatting to achieve a good-looking result. I find code filled with GO-anything a hassle to follow unless it is really formatted strictly.
   ■ The fact that in many cases, too many GOSUBs without RETURNs can cause a stack overflow. I know DBP is an exception, but when I run a crash test, the app still terminates which is still "an undesired result".
   ■ Most people who are new or who haven't coded in a while tend to wind up with a huge mess because of the things I've mentioned. So it's better for newcomers to avoid GOSUB and stick to functions as they teach better disciplines, structured thinking, etc.

I'm not saying GOSUB is without merit. Heck, I use GOSUBs a lot in examples specifically because things don't go out of scope and it allows me to have a label like "Setup" out of plain view which stops all the setting up detracting from the important part. I also see the use of no-scope-change in some situations though I still opt for functions and passing data purely because of taste.
I'll admit that for performance, I occasionally use GOTOs, but I make sure I know exactly where I am and will seldom use it beyond "the GOTO-loop". Oh dear, I'm rambling - break;
Anyhow, just my opinion, really, that's all most advice is; opinion derived from personal experiences and observations.
Heh, people will still be arguing about whether the opening curly brace goes at the end of the line or on a new line another 20 years from now (I've been converted to the end-of-the-line recently).

[OFF-TOPIC]
Yaaay! The PonyCraft sig is back
[/OFF-TOPIC]

RP Functions Library v1.0

My signature has not been erased by a mod.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Dec 2012 01:28
Quote: " Labels don't accept parameters nor return values."

That's not why people use GOSUB, we have functions for that. But not every check of code needs parameters or to return a value.

Quote: "Lack of scope change can wreak havoc with variables with the same name."

Hence why people should use variables with meaningful names.

Quote: "Code debugging can be harder merely due to formatting of labels vs formatting of functions."

Debugging I find is much easier. Code is broken down into various segments, keeping it cleaner and more organized.

Quote: " The fact that in many cases, too many GOSUBs without RETURNs can cause a stack overflow"

That's why we put RETURN at the end of each GOSUB label, and not use GOTO.

Quote: "Lack of scope change doesn't delete temporary local variables."

You'd prefer to constantly create and destroy a variable over and over from inside a function?

To be honest, all these problems are simply caused by incompetent coding and nothing to do with GOSUB itself. (I'm not trying to call you incompetent) I use GOSUB a lot as it allows me to easily see the flow of my program more clearly from the main loop.


Quote: "I occasionally use GOTOs"

Nooooooo! However, like your curly bracket statement, this argument will go on forever!

Quote: "Heh, people will still be arguing about whether the opening curly brace goes at the end of the line or on a new line another 20 years from now (I've been converted to the end-of-the-line recently)."

End of the current line

"You're not going crazy. You're going sane in a crazy world!" ~Tick
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Dec 2012 01:52
Quote: "End of the current line "


Nooooo, because then you don't clearly see which two brackets belong to each other because they're not over each other!

@ nonZero

Honestly, I still don't see why not to use Gosubs. Like Phaelax pointed out, they're a way of organising your code into segments which belong together. My style of coding usually involves something along the line of this:



The reason being, everything to do with morons is within scope of the ControlMorons() function, and also in scope of all other moron segments handled by gosubs.

TheComet

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 2nd Dec 2012 17:50 Edited at: 2nd Dec 2012 17:53
This is gonna be long. Can't think of any short way to word this. Can't even get my words right but if I trip over my tongue, you guys no by now that I don't mean anything in a bad way.


@Phaelax:


Quote: "Quote: " Labels don't accept parameters nor return values."
That's not why people use GOSUB, we have functions for that. But not every check of code needs parameters or to return a value."


True, but why bother differentiating? If a function can be used as a sub and a function and a sub only works as a sub then, again my personal preference, just unify the whole thing. Admittedly I'm biased since for a long time, I've perceived subs kinda obsolete (I'm not alone, subs aren't supported in many languages).


Quote: "Quote: "Lack of scope change can wreak havoc with variables with the same name."
Hence why people should use variables with meaningful names."


Yes, I do use logical names. I have my own system for variables and try to use same-names for same-tasks as it makes tracking easier. So if I'm just storing something temporarily, it could conflict unless I had structured my program to be conflict-proof (which I obviousy do nowadays). But for people new to coding, it's not always easy to get that structuring right, hence my suggestion to simply avoid.

Quote: "Quote: "Code debugging can be harder merely due to formatting of labels vs formatting of functions."
Debugging I find is much easier. Code is broken down into various segments, keeping it cleaner and more organized.
"


I guess that's personal taste I find this:


easier to look at than this (I suppose code-highlighting plays a small role too):

for purely cosmetic reasons. So yeah, my personal taste again (I did say in the previous post that some reasons were subjective). So far as newcomers go, it is unlikely that their subs will be that tidy, nor their GOSUB calls well placed (as was the case with OP's code - no offence OP).


Quote: "Quote: " The fact that in many cases, too many GOSUBs without RETURNs can cause a stack overflow"
That's why we put RETURN at the end of each GOSUB label, and not use GOTO."


GOTO cannot, alone, cause a Stack overflow. GOTO is an unconditional jump without return.

AFAIK, that may loop indefinitely but it won't crash because there's no return address.
The following would cause an overflow if DBP didn't have special handling for it that terminated the application:



Quote: "
Quote: "Lack of scope change doesn't delete temporary local variables."
You'd prefer to constantly create and destroy a variable over and over from inside a function?"


Yes, within reason. I suppose it would ultimately depend on just how often that call will be made. If I'm going to be using a variable a lot, I tend to declare it with global scope and I usually reduce procedures to require the least amount of temp variables in functions that are called regularly. Essentially though, if I don't need the memory, I don't want it hanging around, eg:
With a Function:


Using a label and GOSUB instead:


Quote: "To be honest, all these problems are simply caused by incompetent coding and nothing to do with GOSUB itself. (I'm not trying to call you incompetent)..."


No innuendos read into it, promise. And I agree with you whole-heartedly about the problem not lying with GOSUB. As I mentioned before, "GOSUB is not without merit". The same thing could be said about almost any statement, including GOTO. GOTO is a great optimizer but I will tell most people to stay away from GOTO because it requires a very specific understanding of program flow. Because of the myriad of potential problems I mentioned, I also generally advise against GOSUB.
I'm not saying for a minute that everyone should stop using it completely, just that it's one of the things I advise newcomers against if they seem like they're using it wrong. I'd never have the arrogance to say what coding style is right or wrong, I merely suggest what I feel is most appropriate for the individual at the time (based on the code they post along with any extra info I pick up from their post(s)).

Finally, thanks for giving me some things to think about. There's much validity in what you say and I'm prolly one of the few people who would actually be against GOSUB in BASIC (most people are against GOTO instead, it seems).


@TheComet:


Quote: "

"

You make GOSUB call INSIDE functions!?! Surely you jest, my good sir!!! ... Actually, it's an interesting usage of GOSUB and, tbh, I'm somewhat intrigued out of pure academic curiosity because that shouldn't even compile (And bumblebees shouldn't be able to fly too, lol) so you've given me a proverbial rubix cube to play with now (more distractions *grumble, grumble*).
Here's the thing though:

Putting aside the fact that it compiles and runs, I notice the scope of Bar remains in the scope of the main module. So when I call GOSUB from a function, it's not in the function's scope anymore, but the main module's. So that means you can't alter any of Foo's locals in Bar, however Bar can modify the main module's. That is just a touch dangerous if you're not really careful (I'm sure you are careful if your GC posts regarding that electrical engineering stuff you're into are anything to go by).
Well, even though your method's not compatible with me, I do see where you're coming from regarding the organisation and visual neatness so once again, I spose it does boil down to preference. I guess, as IanM once said, "Pragmatism over dogmatism". If it works and it's comfortable, I suppose it's all good.
Well I'm glad I learned something interesting about DBP from that, so thanks. It seems like just when I think I have this thing waxed, something that shouldn't work turns out to work just fine.

If nothing else, I'm left more open-minded now.

RP Functions Library v1.0

My signature has not been erased by a mod.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Dec 2012 14:58 Edited at: 3rd Dec 2012 15:00
Quote: "Putting aside the fact that it compiles and runs, I notice the scope of Bar remains in the scope of the main module. So when I call GOSUB from a function, it's not in the function's scope anymore, but the main module's. So that means you can't alter any of Foo's locals in Bar, however Bar can modify the main module's. That is just a touch dangerous if you're not really careful (I'm sure you are careful if your GC posts regarding that electrical engineering stuff you're into are anything to go by)."


You've caused me to fly over the many functions in my PonyCraft code and I actually couldn't find one gosub. I must have changed from that style of coding, so I apologise for the confusion (it's funny how you think you did it one way and it turns out you didn't).

That's an interesting discovery you made there btw, that the variables in the subroutine are in the global scope where the variables in the function are local.

I compiled your code and disassembled it with a debugger to see what was up. I found that the memory for all variables are allocated at the very beginning of the program, and are never removed until the program ends. This includes local variables. When entering a subroutine, the variable "x" in the subroutine "Bar" is at a different memory location than the variable "x" in foo().

However, if you do the following:



Both "x" variables are suddenly at the same memory location, and just as expected, var returns a value of 10.

This is yet more fuel to fire my rant against the DBP compiler. Local variables aren't removed from memory ever! They are reset to 0 when a function needing them is called though, but the memory for all variables exists from the very start.

Now that I think of it, it does save you two clock cycles of the CPU which would be needed to allocate space on the stack like normal C/C++ programs do, but the messy way DBP generates machine code cannot be tolerated on any level.

Conclusion

-DBP doesn't actually have anything that can properly be referred to as "scope" (if you look at the machine code).
-Subroutines have access to the variables defined in the same scope they were defined in, hence this would fix your problem:
but wouldn't make any sense.
-DBP's machine code sucks.

TheComet

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 3rd Dec 2012 21:47
Quote: "(it's funny how you think you did it one way and it turns out you didn't)."

Explains how those chimeras I accidentally created came to be.

RP Functions Library v1.0

My signature has not been erased by a mod.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Dec 2012 21:59
Don't you need a philosopher stone for that, not-equal-to-zero alchemist?

TheComet

nonZero
12
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 3rd Dec 2012 22:27
No, chimeras are easy. It's only Human-Transmutation that I need the stone for... Not that I'm confessing to any future plots.

RP Functions Library v1.0

My signature has not been erased by a mod.

Login to post a reply

Server time is: 2024-04-20 07:29:12
Your offset time is: 2024-04-20 07:29:12