Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <iostream> //as usual basic i/o objects
  2. #include <chrono> //for C++11 time like the seconds() line 32
  3. #include <ctime> //C time functions--useful
  4. #include <thread> //for the sleep function line 32
  5.  
  6. using namespace std;
  7. using namespace std::chrono; //the C++11 time library namespace
  8.  
  9. int main() {
  10. time_t st=time(nullptr); //set programme startup time
  11. tm* startTime{localtime(&st)}; //setup a tm structure(for time information based on st(ie: startup time))
  12. tm timeObj;
  13. unsigned hr=0,min=0;
  14.  
  15. cout<<"Enter prescribed hour(24hr clock): ";
  16. cin>>hr; //user enters hour 0 - 23
  17. cout<<"\nEnter prescribed minute: ";
  18. cin>>min; //user enters minute 0 - 60
  19.  
  20. if((hr>=0)&&(hr<=23)&&(min>=0)&&(min<=60)) { //check
  21. timeObj=*startTime; //assign timeObj the startup time
  22. timeObj.tm_hour=hr; //set the hour aspect of the startup time to the user defined one
  23. timeObj.tm_min=min; //set the minute aspect of the startup time to the user defined one
  24.  
  25. //time difference in seconds between user defined time and startup time
  26. double timeDiff=difftime(mktime(&timeObj),st);
  27.  
  28. if(timeDiff<0.0) {
  29. cout<<"Time passed!!!"; //tell the user to be careful
  30. }
  31. else {
  32. this_thread::sleep_for(seconds((int)timeDiff)); //wait for truncated-to-int "time difference" seconds
  33. //this ensures(not effectively) the user is informed on the time
  34. //the drug must be taken
  35. cout<<"\a\a\a\a\a\n\nHey!!! Take your drugs NOW!!!\a\a\a\a\a"; //inform
  36. }
  37. }
  38. delete startTime; //clear stuff
  39.  
  40. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement