Monday, 11 August 2014

Console Based - C++ Clock


Console Based - C++ Clock

Since my Arduino project I have decided to really start brushing up what C++ I know by making a series of short, console based projects that will hopefully progress in difficulty. The first of which is the clock I put together in a short period of time today. This being the first one it was pretty easy and didn't require too much of my time. All the code which I wrote is contained within one source file, it was just more convenient to do so - since there are only two functions really.

Included Header Files

Of course "stdafx.h" was included since I'm using Microsoft Visual Studio.
<iostream> was included so I could use the console writing apparatus(cout, cin, ect).
I included the <string> header since I had recently learned about it and had the idea to really easily create a cheesy "password" piece of functionality at the beginning of the program.
<windows.h> is required for the Sleep() function which when set to 1000 ms would form the basis of the timekeeping.
Initialization  steps


Above is the code that allows the user to enter a password to use the clock, in this case the password is the word "pass" I could have done this by creating an array of char variables but I'm not quite comfortable with that yet, if I was I would be creating a basic encryption program not a clock! Alas, the next few lines allow the user to set the time, including hours, minutes and seconds.  The last line shown is a safety feature in case the user chooses numbers that aren't compatible with the clock, or even letters at that.
 A consequence was included if the password was wrong.

Creating pointers
 Since the counting function would be in an infinite loop I decided it was best to keep the set variables (seconds, minates, hours) in a separate function and point to them from the counting function, which is labeled "check" in my source code.
 When the pointers were first created I "nulled" them, perhaps unnecessarily, by making them equal to 0 before they were assigned any values. Just a good habit to get into, especially according Murphy's Law.

Void check() 


The check function is a rather crude mechanism which, due to its infinite characteristics, I am sure wastes lots of memory each time a local copy of the variables are created. However with the C++ skillset I know its the only way I could think of to get the job done. 
Lines 3 though 8 increment the value of seconds that the user initially specified, taking a one second gap between each and of course printing the result to the screen. All this is able to happen only while the value of seconds is less than 59. when it reaches that number two different things could happen. The minute count could increase by one (lines 12 - 15) or the hour could increase by one (lines 16 -22) the latter of the two inevitably means that the minutes must also change. The last line of code repeats the function but uses the new values for seconds, minutes and hours. So when the function restarts the seconds value will always be O and the other two will have changed accordingly.

Downloads
This project was useful to me because I was able to use pointers in a practical application and involved plenty of transferring variables between functions. For someone who is learning these are both great skills to practice. Not to mention I was able to do it in an afternoon.

If you want to download the program for further inspection or running click here 

My apologies for all the ugly in my code. I'm still learning. Thank you for your time in reading this and until next time.










No comments:

Post a Comment