Advertisement
janac

Timer for minutes and seconds

Dec 15th, 2021 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include<iostream>
  2. #include <ctime> // time
  3. #include <thread> // sleep_for
  4. #include <string> // stoi
  5.  
  6. using namespace std;
  7. using namespace std::this_thread; // sleep_for
  8.  
  9.  
  10. // This function takes a string from the console and
  11. // tries to convert it to an integer. If successful, it
  12. // returns the integer. If an exception is thrown, it
  13. // rethrows the exception to the main function.
  14. int validate_input(string input)
  15. {
  16.    
  17.     // Assume invalid input and validate.
  18.     bool input_is_valid = false;
  19.     int input_int = 0; // String converted to integer.
  20.  
  21.     while (input_is_valid == false) // While the input is invalid,
  22.     {
  23.         try // Watch for exceptions.
  24.         {
  25.             // convert_input_to_integer throws these exceptions:
  26.             // invalid_argument and out_of_range
  27.             input_int = stoi(input); // Convert input to integer.
  28.             input_is_valid = true; // If no exception, input is valid.
  29.         }
  30.         catch (...) // Catch any exception.
  31.         {
  32.             throw; // Get new input from the calling function.
  33.         }
  34.     }
  35.  
  36.     return input_int; // Return the input converted to integer.
  37. }
  38.  
  39. // Wait the requested number of seconds, then
  40. // return to the calling function.
  41. void run_timer(int seconds)
  42. {
  43.     time_t time_start = time(NULL); // Start now.
  44.     // Get the current time and subtract the start
  45.     // time from it. While the difference is less
  46.     // than the requested number of seconds,
  47.     while (time(NULL) - time_start < seconds)
  48.     {
  49.         // Wait one second.
  50.         sleep_for(std::chrono::seconds(1));
  51.     }
  52.  
  53.     // Return to the calling function.
  54.     return;
  55. }
  56.  
  57. int main()
  58. {
  59.     string minutes; // User's requested number of minutes.
  60.     int minutes_int = 0; // Minutes converted to an integer.
  61.     string seconds; // User's requested number of seconds.
  62.     int seconds_int = 0; // Seconds converted to an integer.
  63.     int total_seconds = 0; // Total seconds requested.
  64.     const char beep = 7; // A sound to make when the timer is done.
  65.     bool input_is_valid = false; // Assume invalid input and validate.
  66.  
  67.     cout << "Set the timer.\n"
  68.         "How many minutes? ";
  69.     while (input_is_valid == false) // While the minutes input is invalid,
  70.     {
  71.         try // Watch for exceptions.
  72.         {
  73.             getline(cin, minutes); // Get minutes.
  74.             minutes_int = validate_input(minutes); // Validate input.
  75.             input_is_valid = true; // The input passed validation.
  76.         }
  77.         catch (...) // Catch any exceptions.
  78.         {
  79.             cout << "Number of minutes is invalid. Be sure to enter an integer.\n";
  80.         }
  81.     }
  82.    
  83.     input_is_valid = false;  // Assume invalid seconds input and validate.
  84.     cout << "How many seconds? ";
  85.     while (input_is_valid == false) // While the seconds input is invalid,
  86.     {
  87.         try // Watch for exceptions.
  88.         {
  89.             getline(cin, seconds); // Get seconds.
  90.             seconds_int = validate_input(seconds); // Validate input.
  91.             input_is_valid = true; // The input passed validation.
  92.         }
  93.         catch (...) // Catch any exceptions.
  94.         {
  95.             cout << "Number of seconds is invalid. Be sure to enter an integer.\n";
  96.         }
  97.     }
  98.    
  99.     total_seconds = (minutes_int * 60) + seconds_int; // Calculate total seconds requested.
  100.     cout << "Timer will run now ...\n";
  101.     run_timer(total_seconds); // Run the timer.
  102.     cout << beep << '\n'; // Make a sound when time is up.
  103.     cout << "Time is up!\n";
  104.  
  105.     return 0; // End the program.
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement