Hi everybody! I recently wrote a little simulation program, that simulates a real-time elevator system with 3 elevators. Since the code is around 530 lines I presume it is not a good idea to paste it here. There is some number of minor flaws of logical type and I would be very thankful on any ideas or corrections.

Since I don't posses any web space and I believe that corrections will be (probably) move obvious on a personal machine I can send the code to the email of anybody who's interested. I guarantee that no harmful programs will come with the email, in case somebody may think so.

If somebody is interested please answer to this post or send me a personal message with your email. If you have a better proposition than the email option that I can think of at the moment - please, of course, share it with me :)

Thanks to all in advance!

Member Avatar for iamthwee

Your program is divided into functions right?

Post the functions where you think the flaws are?

They will make no sense without the other code. Besides the program is divided into files, besides the function and namespace division..

Member Avatar for iamthwee

Just post the all the code then?

Member Avatar for iamthwee

Any hints as to where the flaws are?

liblifts.h is the dirty worker in this case so this is the place. I presume that the Elevator class is the primary and probably almost only source of errors. The open_doors() function and the move() function are probably most critical. If I must be sincere - I don't have an exact idea what's wrong .. if I did have one I would try to correct the code myself.

Member Avatar for iamthwee

I bet you know what I'm gonna say don't you.

Let me guess, you wrote the whole program in one go, and then hit compile and execute and expected it to work?

My advice would be to test each function or in your case, each method, separately and make sure it's doing what it is supposed to. Once you've gone through each method systematically you should be able to find out what's going wrong and where.


It's the only way.

Another thing, I noticed, it appears you're not freeing up your memory.

Where's your use of delete[].

And you really need to work on your indentation. It's horrendous. I also hate the use of try catch statements in c++, but that's just me.

Generally you're right about the testing. I can't think of a way, though, to check every function, especially when one function often uses the other from the same class and executable code is produced only when all the used functions are written down. Once the whole picture is clear I can start tracking down the problems.

As for delete[] - this belongs to the last actions I have to do. My primary concern at the moment is that the code starts running as it is supposed to do :-) Optimization belongs to the last stage before giving up the ready code and will be done, I can assure you.

freemind: You know that you're not supposed to put function bodies or variable definitions in a header, right? (Well, now you do.)

I may actually help more later on, but there are many things that make this code difficult to look at and understand. So I'm taking my time.

Member Avatar for iamthwee

>Generally you're right about the testing. I can't think of a way, though, to check every function, especially when one function often uses the other from the same class and executable code is produced only when all the used functions are written down.

Of course you can test each method individually, you just need to think about changing a few bits and bobs. After all how else, do you think everyone else debugs their programs. :!:

Ideally, this is something you should have done from the onset. That is, testing your program stage by stage. Ho ho ho. Lesson learnt.

freemind: You know that you're not supposed to put function bodies or variable definitions in a header, right? (Well, now you do.)

I will read on this topic, thanks.

I may actually help more later on, but there are many things that make this code difficult to look at and understand. So I'm taking my time.

If you are so kind to share these things with me I could use the time to correct the outlook of the program.

Lesson learned but unfortunately the fact persists for our very case. Since I have the 2 options now - either to rewrite everything or to try to correct it as it is I'm a bit more for the second option. Notice taken, though.

As for delete[] - this belongs to the last actions I have to do. My primary concern at the moment is that the code starts running as it is supposed to do :-) Optimization belongs to the last stage before giving up the ready code and will be done, I can assure you.

This is absolute nonsense. A memory leak is not something that you 'optimize' with progressive improvements. Either it leaks and is broken, or it doesn't. If you're not going to write code that you know is correct, it is not surprising that it doesn't work. Go through every function in your code and explain why it works correctly. Write it down. You must be able to do this for every correct function -- thus you'll find where your code fails.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.