| martianxx (33) | |||||
Hi I am trying to draw a digital clock using a console in c++. The idea is I use a 3D array to store 7x7 representations of the numbers 0-9 plus ":" and ".". For example a 5 mike look like the following:
For now I am trying to just output a static hard coded time. Once I have done that i'll capture the system time and turn into an actual clock. The problems I am having are that number 1 although I can write out each number easily they appear under neath each other rather than next to each other. How can I move the cursor back to the top off the screen after I print each number. The next problem will be when I have numbers with multiple digits such as 12 since it is composed of 1 and 2. I guess I need to split that into two numbers and print them both but I am not sure how to (string manipulation?). The final problem is that I wanted to initialize my array that stores the numbers in a separate function but have access to it in main(). (Ie that is where it is declared and it is passed by reference to other functions) however I have issues declaring it in main and then initializing it in another function as I am not sure of the syntax. Here is my current code:
All help welcome. Thanks. | |||||
|
Last edited on
|
|||||
| TheIdeasMan (1564) | |||
This is never executed because done is false and the value of done is not changed. Just delete that segment of code. At least your looks as if it might work for single digits. Printing the numbers side by side is probably the hardest part of the assignment. I think a key step would be to have 2d arrays instead of 3d arrays. You could have another array that can hold just the 4 digits to be printed. So for 4 digits you would have a 28 by 7 array. You could refer to this as the OutputMap. Now you just have to figure out how to fill that array with 0's & 1's for your digits. You could have a 70 by 7 - 2d array to store the digit maps in the first place. This makes it easier to put numbers in the OutputMap, because the row numbers coincide. Then all you have to do is print the output map. There you go - how's that for some ideas? The whole thing should be a piece of cake !!!! | |||
|
|
|||
| chipp (506) | |||
@ideas: i don't understand, you said:
is never executed because done is false, wasn't it (!done) have a true condition?
| |||
|
Last edited on
|
|||
| whitenite1 (705) | |||
|
@martianxx Here is one of the LCD clocks I've done. Maybe what I've done, will give you ideas on doing yours.
| |||
|
|
|||
| Chervil (812) | |
|
@whitenite1 Interesting code, thanks for sharing. One minor problem I've found with using code which executes once per second (using Sleep(1000) in this case) is that it is not likely to keep precise intervals, meaning that occasionally the "seconds" part of the display may update with a gap longer or shorter than 1 second. A way round this is to use a shorter timing interval than 1000ms. Then each time around the loop, get the current time and compare the "seconds" value with the value stored previously. Only when the "seconds" value changes is there any need to update the display, the other times, simply bypass the bulk of the code and go back to sleep. | |
|
|
|
| whitenite1 (705) | |
|
@Chervil Yes, you're right. The program does do the skipping part, every now and then. But since this program was jst for looks, and not actually NEEDING to be precise, I figured it's good enough. I have another LCD Clock program, with colors, hex display, binary display, regular clock face, and 5 different LCD number designs, along with military and 12 hour displays. Has the month, day and year, week number and days into the year. Had a lot of fun programming it. | |
|
|
|
| martianxx (33) | |||
Thanks for the ideas I combined both @TheIdeasMan and @whitenite1 of them (the idea for an out but "buffer" and some of the layout of whitenite1's code. Here is what I now have:
Although this works parts of it are very un-necessary and so I will keep working on it until I feel it is right (foremost that hard coded switch statement). Also I think I declared my arrays incorrectly as their output was back to front. Is there a better way for splitting a double digit number into its components. I get the impression my solution is far from efficient. I may remove the output "buffer" altogether and utilize the 3rd dimension of my array to output all of the first row of all the numbers before moving onto the next row as it probably saves time. | |||
|
|
|||
| martianxx (33) | |||
Here is what I have modified it to:
| |||
|
|
|||
| whitenite1 (705) | |||||
|
@martianxx Very nicely done. I like the small blocks making up the numbers. Looks better than the asterisks, in my opinion. If you wish to move the time display into the center of screen, Just change
to..
| |||||
|
|
|||||
| martianxx (33) | |||
Thanks. Here is what I have ended up with:
All in all a nice project and I learnt quite a bit. | |||
|
|
|||
| TheIdeasMan (1564) | ||
|
martianxx Good work!! One tiny extra thing - You could put the initialisation of the array in to it's own header file. It is a constant thing so you could get away with this I think. Plus the other const's you have before main. The definitions of the functions could go in separate file as well. So this leads me to think - why not put it all into a class. It is quite clear to me that you have good knowledge of how to program, so starting to learn classes would be a good step. I am guessing you haven't used classes before, so a quick description of how it works. Apologies if you know all this already. The normal arrangement for this is a header file (CClock.h say)for the declaration of the class, a CClock.cpp for the definition of the functions, and a Main.cpp file which would create a CClock object and call it's functions to interact with it. Initialisation of variables is done with a constructor. Don't fall into the trap of providing public get / set functions for each member variable. There is an article on classes in the documentation section.
Hope all goes well !! :+) | ||
|
|
||
| martianxx (33) | |
|
Your right I have coded before :) I am studying computer programming (and science in general) at university. I have used classes heavily before in a platform game that I made and understand all the normal OO stuff such as inheritance, constructors, destructors as well as how to define a class in a header file etc. This was actually one of the exercises is a long list of exercises that I have extrapolated upon. Since I have gone this far I agree it would be a lot better in a class and would be better as a class to keep in my portfolio as it would be more portable. I will do this and post it. I also want to clean the code up a bit, a few in consistencies such as un-capitalized "d" in the draw() function when the rest uses camel case. EDIT: I still cannot work out why my array initialization has to be back to front to get the numbers displaying correctly. | |
|
Last edited on
|
|
| martianxx (33) | |||
Here is my clock class. I fiddled around for a while trying to declare the array as a part of the class but ended up declaring it outside of the class. If anyone knows a way to declare the array within the class and give it values I would be very interested:
| |||
|
|
|||
| TheIdeasMan (1564) | |||
You can declare the array with the size of the subscripts in the header file. Initialisation can be done in the default constructor, so this will execute when you create a clock object.
Without even looking, that has to be the execution of the for loops in the Draw function. You will just have to play around with these, to figure out the correct order. | |||
|
|
|||
| martianxx (33) | |
| Thanks have it all fixed and finished. | |
|
|
|