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 / Problem with array searching

Author
Message
Wiley85
16
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 29th Apr 2009 21:40
In building a more serious, non-game program I have encountered a strange problem. My project is a simple address book which takes user defined information and stores it in an array, then allows the user to adjust this information later.



As you can see, I haven't posted the entire code, just case one and two, but the rest of the code searches the array in the same manner. The specific problem I am having is this: I run the program and it lets me enter new information just fine, and I've added a diagnostic to show that the program actually stores the info. I then go back to edit the info using case two, and the program is supposed to take the user input, compare it to the info in the array, and if they are equal it jumps to another place in the program. However, every tine I run it, for whatever reason it can't see that the two are the same and simply moves on to display the "No matching info found" part. I've added another diagnostic here to show that the program actually DOES look at each item in the array. Can anyone help me with this very weird problem? Thank you for your posts and time.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 29th Apr 2009 23:00
start with the simple answer, are you saving and loading the array using save array and load array?

There are only 10 kinds of people in the world, those who understand binary and those who dont
Wiley85
16
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 29th Apr 2009 23:28
Yes, and actually I looked at it a little more and tried something that worked. I switched the order of the equivalency statements i.e. book$(var,2)=sl$ turns into sl$=book$(var,2). I also had to change i=var to var=i in the add info part. Dunno why that is, but it worked.
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Apr 2009 01:59
In this bit of code:



Var has no value when you are assigning i it's value. I think your intention is to use the value of i from the loop to assign an index of where to store the new book in your list of 100. Because in your array: subroutine you are using var as the index, it is the variable var that has to carry i's value - so the correct assignment is var=i. I only see an if statement using sl$=book(var,2). And sl$ is being assigned a value before the test so I don't know why the order is making a difference. Perhaps you had used s1$ (pronounced: ess one) instead of SL$

As far as making life easier for yourself and others that may need to read your code, If you can avoid using goto and use some other means for executing subroutines, you may be able to track the flow of your code better as it gets more complex. Here's one alternative:



I basically moved the subroutines to their our section. all grouped together. In your code, I used gosub where appropriate so that after the subroutine is exectued, the program returns to the spot from where the subroutine was called. If it was a loop, I use the command exit after the gosub so that the program doesn't continue running through the loop.

Enjoy your day.
thenerd
16
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 30th Apr 2009 02:02
i'm not sure what that is, but I have a question?. why DB? it wasn't designed for that kind of stuff (even though BASIC was).
I have made a program generally like what you were describing, only I used python, which can easily do that kind of things in a snap.

Now is better than never.
Although never is often better than *right* now.
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 30th Apr 2009 17:24 Edited at: 30th Apr 2009 17:28
Quote: "I have made a program generally like what you were describing, only I used python, which can easily do that kind of things in a snap.
"


Thats exactly the reason! what do you think is more fun? making a program with something made to do it(it's really really easy to do) or make one in something thats not(a lot harder!)

in my oppinion the harder one would be more fun/a lerning experience since you haVe to have the basics down GREAT to do things like this in db.


here's a example:

if you search GUI in the db forums you will get abunch of gui functions made in db. now some of them might not look great BUT think of how much more fun that is then using some dll to create you gui effect.(or learning/using C++ or something even though c++ is Fun )

New Site! Check it out \/
Wiley85
16
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 30th Apr 2009 22:06
Heh that's a really good point, but the reason I used DB is simple: it's all I have, and I'm just learning to program. I was given DB for christmas and have basically been teaching myself how to do it. And I've never heard of python before, unless we're talking about Monty Python he he.

And as for subroutines, I initially thought I should use them, but simply found the goto's easier to manipulate. I will however, for sake of learning, try to write another version of my program using subroutines and see if I can make it work. Thank you all for the excellent posts and advice.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 1st May 2009 01:49
im not sure exactly why but ive been told u should avoid gotos as much as possible, ask some1 why

There are only 10 kinds of people in the world, those who understand binary and those who dont
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 1st May 2009 05:15 Edited at: 1st May 2009 05:19
i never herd that. although i prefer functions to subroutines myself.

i don't like having to type Gosub SubName(or goto) i'd rather just type what i need rite there as the command

New Site! Check it out \/
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st May 2009 07:48
gosub vs gotos

a gosub jumps to a subroutine and then returns to exactly where it jumped from. Gosub can help keep order to the program flow.

goto jumps somewhere without a return. As a program gets more and more complex, it can become very difficult to figure out the flow of the program and debugging can be a nightmare. Gotos can be useful inside of functions or inside of subroutines, but once they start branching around to the main program and other subroutine calls it can become a mess to trace.

Enjoy your day.
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 1st May 2009 17:56
Oh ya i geuss that makes sense. Learns something new every day

New Site! Check it out \/
gamerboots
16
Years of Service
User Offline
Joined: 8th Dec 2008
Location: USA
Posted: 6th May 2009 11:20
Quote: " not sure exactly why but ive been told u should avoid gotos as much as possible"

ok, I'll shed some light on this,Latch is pretty much correct except for one thing. when using goto, the program not only jumps but it forgets where it was. Its like stepping through a time portal - everything that happened before you jumped is forgotton. Secondly , it does not return back to the caller,and since it dont remember anything Its a sure way to doom any large projects as any resources allocated are still in memory. Now imagine if you will for a second that in part 1 of a program (before you use a goto) that you allocate an array of 4000 variables. you then jump to part 2 of the program and allocate another 4000, but you have to get back to part 1 so you jump back to part 1 which will then jump over to part 2. so your program is going to allocate yet another 4000 variables in part 1 , then jump to part 2 and allocate another 4000 (and the cycle continues until you are out of memory). These unseen cycles can devistate your program and cause anything from an unknown bug that Crashes the program to the desktop to a worse case scenerio of your computer's opertating loosing stability and rebooting. These bugs are very hard to track because the program jumps and appears to work like it should , but in the background (unknown to the begining programmer )it sucks more and more resources out of the system until theres nothing left. I therefore caution you not to use them without exteme care and encourage you to use functions and sub-routines as I estimate the failure rate of any project that has a single goto in it to be approximately 92%

Regards
Emperor Gamerboots~
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 6th May 2009 17:56
nice rant Gamer Boots

however dont throw out gotos completely, they are good for simple small stuff like this:

like if you need to change a flag



ya i know u could use this format



but im just using this as an example

There are only 10 kinds of people in the world, those who understand binary and those who dont
Wiley85
16
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 6th May 2009 20:56
That's a good point. I would like to think that if gotos were completely useless, they wouldn't be a part of the code and would have been eliminated long ago. I've re-written my code to use subroutines instead, but still found gotos useful for small stuff like that one smart guy demonstrated. Check it out and tell me if this looks any better than before:




If you look in the Edit Information subroutine, you'll find my only goto in case seven. Thanks again for all the excellent criticisms and posts!
Wiley85
16
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 6th May 2009 20:57
Scratch that, I found two gotos, really close together. Sorry for the misinformation!

Login to post a reply

Server time is: 2025-08-08 20:38:38
Your offset time is: 2025-08-08 20:38:38