Jump to content

So what i've got so far:

int month, hour, minute,sec;     // get current time    time_t alarm = time(0);    // convert to 'broken time'    tm clock = *localtime(&alarm);    cout<<"*********Alarm Clock v0.1*********\n";    cout<<"Enter the hour and minutes to set the alarm"<<endl;    cin>>hour>>minute;    for(int i=0; i<5000; i++)    {        system("cls");        cout<<"*********Alarm Clock v0.1*********\n"<<endl;        cout<<"           "<<clock.tm_hour<<" : "<<clock.tm_min<<" : "<<clock.tm_sec++;        if(clock.tm_sec==60)// manually adjust time        {            clock.tm_sec=1;            clock.tm_min++;        }        if(clock.tm_min==60)        {            clock.tm_min=0;            clock.tm_hour++;        }        if(clock.tm_hour==hour && clock.tm_min==minute)           {               system("temp.mp3");               break;           }       _sleep(1000);    }

The main issue is that now i adjust the time manually (i pause the loop for 1 sec and then add to the clock.tm_sec and min), because when the console is "summoned" and the loops is being paused the time does not change. Second issue is that when program is launched and we lets say spent 7 seconds typing the alarm time, the count will be 7 seconds late from the current live time.

 

I'd need to make the console "active" and get the current time live OR somehow remove the changes between live time and console launch time.

 

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

 

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/
Share on other sites

Link to post
Share on other sites

so.. i'm not gonna touch any of your coding, and just gonna mention this:

 

you NEVER want your clock's speed be reliant on the execution speed of a loop. sleep() functions are hilareously unrealiable, certainly over long periods of time with many executions.

 

the *ONLY* case where thats acceptable is when your code has 100% control over the processor its running on, AKA when you're programming assembly on a microcontroller.

 

EDIT: ps: its generally accepted in the manufacturing of clocks for the AC signal coming from the wall to be the most reliable source of speed.

for software polling the system time will be your best bet. your OS's time mechanism is backed by hardware designed to keep time running accurate, even over long periods in storage.

 

i pulled a machine off a shelf thats been collecting dust for a few years, booted it up to a 2 minute time difference with actual time.

not bad for a 20 year old box with an undead bios battery, right?

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6658481
Share on other sites

Link to post
Share on other sites

Saw that code, Horrid reflections of AP computer science homework due the next day and staying up till 4 am attempting to de-bug code............Info Tech for me boys. :)

 

 

Edit: I'm almost completely positive, you could google and find similar working code, and compare yours trying to visually see the problem.

Edited by Chris_R.

Thanks!

 

Chris R.

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6658577
Share on other sites

Link to post
Share on other sites

so.. i'm not gonna touch any of your coding, and just gonna mention this:

 

you NEVER want your clock's speed be reliant on the execution speed of a loop. sleep() functions are hilareously unrealiable, certainly over long periods of time with many executions.

 

the *ONLY* case where thats acceptable is when your code has 100% control over the processor its running on, AKA when you're programming assembly on a microcontroller.

 

EDIT: ps: its generally accepted in the manufacturing of clocks for the AC signal coming from the wall to be the most reliable source of speed.

for software polling the system time will be your best bet. your OS's time mechanism is backed by hardware designed to keep time running accurate, even over long periods in storage.

 

i pulled a machine off a shelf thats been collecting dust for a few years, booted it up to a 2 minute time difference with actual time.

not bad for a 20 year old box with an undead bios battery, right?

live and learn, live and learn.

Eliminating sleep() will now crush my somewhat working clock. How the heck i replace it?

I mean time ( ctime(&alarm) ) does not change in console since it's triggered.

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

 

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6658659
Share on other sites

Link to post
Share on other sites

@MisterWhite

This is what I did, it's pretty close to what you want to do . I use std::chrono.

#include <chrono>#include <sstream>#include <iostream>#include <iomanip>#include <thread>int main(){    std::tm tm;    std::stringstream ss("Dec 3 2015 23:41:00");    ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");    auto t = std::chrono::system_clock::from_time_t(std::mktime(&tm));    std::this_thread::sleep_until(t);    std::cout << '\a';    std::cin.get();    return 0;}

It uses stuff from C++11 (maybe 14?) . It does work as it should. Ok , I haven't handled the user input , but that should be really easy. I just wrote it into the source directly.

 

What it does is basically gets a certain timepoint , from a string (stringstream actually , but you get it) , then makes the thread to sleep until that time point.
Then it just does the usual "beep" sound. You can make it play anything you'd want honestly.

 

Or, if you also want to display the remaining time, you could make a loop in which to just constantly check if the duration between the time point which came as input , and std::chrono::system_clock::now() , and to constantly display the duration as a string.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6660791
Share on other sites

Link to post
Share on other sites

@MisterWhite

This is what I did, it's pretty close to what you want to do . I use std::chrono.

#include <chrono>#include <sstream>#include <iostream>#include <iomanip>#include <thread>int main(){    std::tm tm;    std::stringstream ss("Dec 3 2015 23:41:00");    ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");    auto t = std::chrono::system_clock::from_time_t(std::mktime(&tm));    std::this_thread::sleep_until(t);    std::cout << '\a';    std::cin.get();    return 0;}

It uses stuff from C++11 (maybe 14?) . It does work as it should. Ok , I haven't handled the user input , but that should be really easy. I just wrote it into the source directly.

 

What it does is basically gets a certain timepoint , from a string (stringstream actually , but you get it) , then makes the thread to sleep until that time point.

Then it just does the usual "beep" sound. You can make it play anything you'd want honestly.

 

Or, if you also want to display the remaining time, you could make a loop in which to just constantly check if the duration between the time point which came as input , and std::chrono::system_clock::now() , and to constantly display the duration as a string.

i get a lot of errors :D, i guess because of "C++11 (maybe 14?)"

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

 

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6671922
Share on other sites

Link to post
Share on other sites

i get a lot of errors :D, i guess because of "C++11 (maybe 14?)"

Probably.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/498228-alarm-clock-c-help/#findComment-6671990
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

×