Coders Packet

Creating an alarm clock in C++

By John Jayakumar H

  • Alarm Project.cpp
  • Hi friends, Today let us see how to create an alarm clock in C++ using inbuilt Beep function

    Here is the code:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    class alarm {
    public:
        tm alarmtime,  presenttime, differenceintime;
        time_t now;
        alarm() 
        {
        now = time(0);
        presenttime = *localtime(&now);
        alarmtime =  presenttime;
        alarmtime.tm_sec = 0;
        }
        void getTime() 
        {
            string time;
            cout<<"Enter Alarm Time in 24-hour format [HH:MM]: "; 
    getline(cin,time); int pos = 0, h; while ((pos = time.find(':')) != string::npos) { string token = time.substr(0,pos); stringstream convert (token); convert >> alarmtime.tm_hour; time.erase(0,pos+1); } stringstream convert (time); convert >> alarmtime.tm_min; } void timeDifference() { int sec = difftime(mktime(&alarmtime),now); differenceintime.tm_min = sec / 60; differenceintime.tm_hour = differenceintime.tm_min / 60; differenceintime.tm_min = differenceintime.tm_min % 60; differenceintime.tm_sec = sec % 60; if (sec < 0) { differenceintime.tm_hour = 24 + differenceintime.tm_hour - 1; differenceintime.tm_min = 0 - differenceintime.tm_min; differenceintime.tm_sec = 0 - differenceintime.tm_sec; } } void start_alarm() { while (true) { system("cls"); now = time(0); presenttime = *localtime(&now); timeDifference(); cout<<"TIME REMAINING: "<<differenceintime.tm_hour<<":"<<differenceintime.tm_min<<":"<<differenceintime.tm_sec; if (differenceintime.tm_hour == 0 && differenceintime.tm_min == 0 && differenceintime.tm_sec == 0) { cout<<endl<<"Time Ended"<<endl<<">>> Press CTRL+C or Close the window to stop the alarm <<<"<<endl; break; } } } }; int main() { alarm a; a.getTime(); a.start_alarm(); for (int i = 0; ; i = i + 50) Beep(i,1000); return 0; }

    The Output for the code is :

                 

    UNDERSTANDING THE ALGORITHM:

    + alarmtime - To set the time at which alarm will ring

    + presenttime - To find the current time of system

    + differenceintime - To find the difference between presenttime & alarmtime

    We create the function getTime() to get the alarm time as input. Here we get the input as a string. Then we create the function timeDifference() to calculate the time difference between the local system time and the alarm time. This is done by using difftime which calculates differences between two timesand gives the result in seconds. So we convert the hours and miniutes into seconds. Then we create the function start_alarm() which gives a beeping alarm sound when the difference time becomes 0. In this function we use the Beep() function which is used to make a Beep sounds. We can also add other musics in start_alarm() to get different music during alarms.

    Download Complete Code

    Comments

    Comments for the packet "Creating an alarm clock in C++";
    No comments yet