Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Advance: Alarm Clock

Advance: Alarm Clock | Basics of C++ - Software Development PDF Download

Introduction

This document provides a beginner-friendly explanation of the C++ alarm clock program. The program allows the user to set an alarm for a specific time, and it continuously checks the system's current time until the alarm time is reached. Once the set time is matched, the program triggers an alarm message.

Code Breakdown

1. Import Required Libraries

#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>

  • iostream: Used for input and output operations.
  • thread: Enables multi-threading to run functions in parallel.
  • chrono: Provides time-related utilities.
  • ctime: Used to access the system's current time.

2. Alarm Clock Function

void alarmClock(int setHour, int setMinute, int setSecond) {
    while (true) {        time_t now = time(0);
        tm *ltm = localtime(&now);      
        int currentHour = ltm->tm_hour;
        int currentMinute = ltm->tm_min;
        int currentSecond = ltm->tm_sec;       
        cout << "Current Time: " << currentHour << ":" << currentMinute << ":" << currentSecond << endl;        
        if (currentHour == setHour && currentMinute == setMinute && currentSecond == setSecond) {
            cout << "\n*** Alarm ringing! Time to wake up! ***\n" << endl;
            break;
        }
        this_thread::sleep_for(chrono::seconds(1));
    }
}

  • The function enters an infinite while loop to continuously check the time.
  • time(0) retrieves the current system time.
  • localtime(&now) converts it into hours, minutes, and seconds.
  • The program compares the current time with the user-defined alarm time.
  • If the times match, it prints an alarm message and breaks the loop.
  • The this_thread::sleep_for(chrono::seconds(1)) function pauses execution for one second before checking the time again.

3. Main Function

int main() {
    int hour, minute, second;    
    cout << "Enter alarm time (HH MM SS): ";
    cin >> hour >> minute >> second;    
    cout << "\nAlarm set for " << hour << ":" << minute << ":" << second << "\n";    
    thread alarmThread(alarmClock, hour, minute, second);
    alarmThread.join();    
    return 0;
}

  • The program asks the user to enter an alarm time.
  • The input is stored in hour, minute, and second variables.
  • A new thread is created to run the alarmClock function, ensuring that the main program remains responsive.
  • alarmThread.join() waits for the alarm function to complete before exiting the program.

How to Run the Program

  • Open a C++ development environment (VS Code, Code::Blocks, or any compiler).
  • Copy and paste the code into a new .cpp file.
  • Compile and run the program.
  • Enter the desired alarm time in HH MM SS format.
  • The program will display the current time and trigger the alarm at the set time.

Conclusion

This simple C++ alarm clock demonstrates the use of time functions, loops, and multi-threading. It continuously checks the time until the alarm goes off. The knowledge gained from this program can be applied to build more complex time-based applications in C++.

The document Advance: Alarm Clock | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
71 videos|48 docs|15 tests
71 videos|48 docs|15 tests
Download as PDF

Top Courses for Software Development

Related Searches

Exam

,

mock tests for examination

,

pdf

,

Extra Questions

,

Important questions

,

Semester Notes

,

past year papers

,

study material

,

Advance: Alarm Clock | Basics of C++ - Software Development

,

Sample Paper

,

ppt

,

Viva Questions

,

Objective type Questions

,

Previous Year Questions with Solutions

,

Advance: Alarm Clock | Basics of C++ - Software Development

,

video lectures

,

Free

,

shortcuts and tricks

,

practice quizzes

,

MCQs

,

Advance: Alarm Clock | Basics of C++ - Software Development

,

Summary

;