Jump to content

Alarm clock C++ (need help)

MisterWhite

I want to create an Alarm Clock program. (i have fairly low c++ experience so don't do advanced algorithms ).

So i just introduced myself to <ctime> library, i got the the most simple example of how to display current date:

    time_t now = time(0);    string dt = time(&now);

So problems i've encountered:

1) How to extract Hours, Minutes or month from this string?

2)The program (i believe) should run until the entered time is reached. So not a simple console pop-out on execution.

3)When time is reached a tune should be played (as in real Alarm clocks :P).

 

I don't need solutions, just links to where i can get info or your suggestions.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

First of all you cannot assign value of time function to string type, read this to see what values does time function return http://www.cplusplus.com/reference/ctime/time/ also you will know why you need only one call (you did the same thing two times in your snippet, which is call  time and save it to now, and then you get time you try to assign int to dt and then again to now).
 
If you want convert time to string and by the way format it to eg. "hh:mm" you use other function from ctime http://www.cplusplus.com/reference/ctime/strftime/
 
you can do it all in loop, but considering time changes only once per second with seconds resolution, there is no sense to loop doing thousands iterations of same time, you can sleep the thread eg. 250-500ms every loop so your clock wont be cpu intensive. This post shows how it can be done http://stackoverflow.com/questions/4184468/sleep-for-milliseconds/10613664#10613664
 
About the alarm, you can use a tick which is output "\a" character to console - it is a "sound" character that makes beep sound, when the alarm starts you can write the character once per second, it is important to not write thousands of those in a second, cause app will queue them up together and it will hand and beeping for another hour, so write one wait for it to end playing then beep another.
 
Last thing is how you would enter alarm time, my guess is to use some function that would convert time as a string to time as time_t (number of seconds) but you would need to be very specific when comes to entering time as string, so the function could parse it. Easier way would be prompting user to enter hours, minutes and seconds, all in separate std::cin calls, then call time function and add those to it so you will have relative (from now) alarm time, and when time reach it it will fire alarm up. the formula is 

time_t alartTime = time(NULL) + seconds + minutes * 60 + hours * 3600;

If you would like to rather use some c++ string to time_t parse function, google it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×