C++ | Alarm Clock
Hello peeps! Lord Hypersonic greets you. Today I am presenting you an alarm clock program in c++.
In this program, you have to enter the time at which you want the alarm to ring. It takes input in 24-hours format, and you have to enter hours and minutes like this "HH:MM". After that, it will start the timer until the clock hit the time which the user enters. When the timer is finished it will print the message "TIME COMPLETED" and will start beeping. The frequency of beep will increase by 50 after every one second.
To stop the alarm clock or Beep sound, press CTRL+C or click on the cross button.
Now let's see, how it works?
First, let us discuss the algorithm to find the difference between askedTime and currentTime.
In this program, you have to enter the time at which you want the alarm to ring. It takes input in 24-hours format, and you have to enter hours and minutes like this "HH:MM". After that, it will start the timer until the clock hit the time which the user enters. When the timer is finished it will print the message "TIME COMPLETED" and will start beeping. The frequency of beep will increase by 50 after every one second.
To stop the alarm clock or Beep sound, press CTRL+C or click on the cross button.
Now let's see, how it works?
Algorithm
We are going to use three structures of tm (tm askedTime, currentTime, differenceInTime; ).
- We will use askedTime to take input from the user (time at which alarm will ring).
- currentTime will be used to store the current time of the system.
- differenceInTime will be used to store the difference between askedTime and currentTime.
First, let us discuss the algorithm to find the difference between askedTime and currentTime.
timeDifference()
1. First, declare an integer variable seconds and initialize it with the difference between the total number of seconds in askedTime and the total number of seconds in the currentTime. Since askedTime and currentTime are the structure of tm, so we can use member function difftime() to calculate the difference between both time periods ( int seconds = difftime(mktime(&askedTime),now); ).
2. Now make differenceInTime.tm_min = seconds / 60;
2.1. Make differenceInTime.tm_hour = differenceInTime.tm_min / 60;
2.2. Make differenceInTime.tm_min = differenceInTime.tm_min % 60;
2.3. Make differenceInTIme.tm_sec = seconds % 60;
3. The above statements will calculate the difference between two time periods i.e., askedTime and currentTime and store the values in the structure differenceInTime.
2. Now make differenceInTime.tm_min = seconds / 60;
2.1. Make differenceInTime.tm_hour = differenceInTime.tm_min / 60;
2.2. Make differenceInTime.tm_min = differenceInTime.tm_min % 60;
2.3. Make differenceInTIme.tm_sec = seconds % 60;
3. The above statements will calculate the difference between two time periods i.e., askedTime and currentTime and store the values in the structure differenceInTime.
If the time period entered by the user is between the current time and 24:00, then the value of seconds will be positive.
If the time period entered by the user is not between the current time and 24:00, then the value of seconds will be negative.
4. If the value of seconds if less than 0 then:-
4.1. Make differenceInTime.tm_hour = 24 + differenceInTime.tm_hour - 1;
4.2. Make differenceInTime.tm_min = 0 - differenceInTime.tm_min;
4.3. Make differenceInTime.tm_sec = 0 - differenceInTime.tm_sec;
Since in step 2 the difference was calculated if the difference calculated is negative that means the time period entered by the user is not between the current time of the system and .24:00:00. In this case, step 4 will be executed to find the exact difference between both time periods.
For example, the current system time is 23:57:00
1. If the time period entered by the user is: 23:58:00, then the difference will be 00:01:00
2. If the time period entered by the user is : 22:00:00 then the difference will be -1:-57:00
So in the second case, the difference is negative, so:-
- We will add 23 with - 1, it will be 24 - 1 - 1 = 22
- We will subtract 0 with - 57, it will be 0 - (-57) = 57
- 0 will remain the same.
This will calculate the difference between askedTime and CurrentTime.
start_alarm()
This function is used to start the alarm.
1. Start an infinite loop.
1.1. Clear the screen ( system("cls"); )/
1.2. Extract current system time in currentTime structure.
1.3. Call timeDifference() function to calculate the difference between askedTime and currentTime.
1.4. Print the difference between the two time periods.
1.5. Check if the difference between the two time period is zero or not, if zero then:-
1.5.1. Print the message TIME COMPLETED or if you want to add any music or sound then add it here.
1.5.2. Break the loop.
That's it, This is the alarm.
To understand the algorithm more clearly and to see how I implement the algorithm, read the souce code.
Source Code
/*
Program: Alarm Clock
Description: An alarm clock which will start beeping when the clock will hit the time which was entered by the user.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.com
*/
#include <iostream>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
using namespace std;
//alarm clock class
class alarm {
private:
tm askedTime, currentTime, differenceInTime;
time_t now;
public:
alarm() //constructor of the class
{
now = time(0);
currentTime = *localtime(&now);
askedTime = currentTime;
askedTime.tm_sec = 0;
}
void getTime() // function to take input from the user.
{
string Time;
cout<<"Enter 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 >> askedTime.tm_hour;
Time.erase(0,pos+1);
}
stringstream convert (Time);
convert >> askedTime.tm_min;
}
void timeDifference() // function to calculate difference between current time and asked time.
{
int seconds = difftime(mktime(&askedTime),now);
differenceInTime.tm_min = seconds / 60;
differenceInTime.tm_hour = differenceInTime.tm_min / 60;
differenceInTime.tm_min = differenceInTime.tm_min % 60;
differenceInTime.tm_sec = seconds % 60;
if (seconds < 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() // function to start alarm and print the time reaming to hit the desired time.
{
while (true)
{
system("cls");
now = time(0);
currentTime = *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 Completed"<<endl<<">>> PRESS CTRL+C or Click on CROSS BUTTON 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;
}
Program: Alarm Clock
Description: An alarm clock which will start beeping when the clock will hit the time which was entered by the user.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.com
*/
#include <iostream>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
using namespace std;
//alarm clock class
class alarm {
private:
tm askedTime, currentTime, differenceInTime;
time_t now;
public:
alarm() //constructor of the class
{
now = time(0);
currentTime = *localtime(&now);
askedTime = currentTime;
askedTime.tm_sec = 0;
}
void getTime() // function to take input from the user.
{
string Time;
cout<<"Enter 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 >> askedTime.tm_hour;
Time.erase(0,pos+1);
}
stringstream convert (Time);
convert >> askedTime.tm_min;
}
void timeDifference() // function to calculate difference between current time and asked time.
{
int seconds = difftime(mktime(&askedTime),now);
differenceInTime.tm_min = seconds / 60;
differenceInTime.tm_hour = differenceInTime.tm_min / 60;
differenceInTime.tm_min = differenceInTime.tm_min % 60;
differenceInTime.tm_sec = seconds % 60;
if (seconds < 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() // function to start alarm and print the time reaming to hit the desired time.
{
while (true)
{
system("cls");
now = time(0);
currentTime = *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 Completed"<<endl<<">>> PRESS CTRL+C or Click on CROSS BUTTON 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;
}
Thanks a lot. Really good
ReplyDeleteMention not :D
ReplyDelete