You are on page 1of 7

ALARM CLOCK

TIME IS VALUABLE BECAUSE IT PROVIDES US


WITH THE OPPORTUNITY TO ACHIEVE OUR
GOALS, BUILD RELATIONSHIPS, PURSUE OUR
PASSIONS, AND TAKE CARE OF OUR
HEALTH.

IT IS A RESOURCE THAT WE SHOULD USE


WISELY TO LIVE A FULFILLING AND
MEANINGFUL LIFE.
PROGRAM TO DEMONSTRATE ALARM CLOCK

• #include <iostream>
• #include <string>
• #include <windows.h>
• #include <stdlib.h>
• #include <time.h>
• #include <sstream>
• using namespace std;
• class alarm {
• public:
• tm alarmtime, presenttime, differenceintime; //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
• time_t now;
• alarm()
• {
• now = time(0);
• presenttime = *localtime(&now);
• alarmtime = presenttime;
• alarmtime.tm_sec = 0;
• }
• void getTime() //Function gettime - To get the alarm time as input and we get input as string
• {
• 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() //Function timeDifference() – To calculate the time difference between the local
system time and alarm time
• {
• //Difftime - To calculates differences between two times and gives the result in second
• So we convert hour and minute into second
• 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() //Function start_alarm() - To gives a beeping alarm sound
• { 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); // Beep function - To make a beep sound.
• return 0;
• }

You might also like