Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <ctime> // time
- #include <thread> // sleep_for
- #include <string> // stoi
- using namespace std;
- using namespace std::this_thread; // sleep_for
- // This function takes a string from the console and
- // tries to convert it to an integer. If successful, it
- // returns the integer. If an exception is thrown, it
- // rethrows the exception to the main function.
- int validate_input(string input)
- {
- // Assume invalid input and validate.
- bool input_is_valid = false;
- int input_int = 0; // String converted to integer.
- while (input_is_valid == false) // While the input is invalid,
- {
- try // Watch for exceptions.
- {
- // convert_input_to_integer throws these exceptions:
- // invalid_argument and out_of_range
- input_int = stoi(input); // Convert input to integer.
- input_is_valid = true; // If no exception, input is valid.
- }
- catch (...) // Catch any exception.
- {
- throw; // Get new input from the calling function.
- }
- }
- return input_int; // Return the input converted to integer.
- }
- // Wait the requested number of seconds, then
- // return to the calling function.
- void run_timer(int seconds)
- {
- time_t time_start = time(NULL); // Start now.
- // Get the current time and subtract the start
- // time from it. While the difference is less
- // than the requested number of seconds,
- while (time(NULL) - time_start < seconds)
- {
- // Wait one second.
- sleep_for(std::chrono::seconds(1));
- }
- // Return to the calling function.
- return;
- }
- int main()
- {
- string minutes; // User's requested number of minutes.
- int minutes_int = 0; // Minutes converted to an integer.
- string seconds; // User's requested number of seconds.
- int seconds_int = 0; // Seconds converted to an integer.
- int total_seconds = 0; // Total seconds requested.
- const char beep = 7; // A sound to make when the timer is done.
- bool input_is_valid = false; // Assume invalid input and validate.
- cout << "Set the timer.\n"
- "How many minutes? ";
- while (input_is_valid == false) // While the minutes input is invalid,
- {
- try // Watch for exceptions.
- {
- getline(cin, minutes); // Get minutes.
- minutes_int = validate_input(minutes); // Validate input.
- input_is_valid = true; // The input passed validation.
- }
- catch (...) // Catch any exceptions.
- {
- cout << "Number of minutes is invalid. Be sure to enter an integer.\n";
- }
- }
- input_is_valid = false; // Assume invalid seconds input and validate.
- cout << "How many seconds? ";
- while (input_is_valid == false) // While the seconds input is invalid,
- {
- try // Watch for exceptions.
- {
- getline(cin, seconds); // Get seconds.
- seconds_int = validate_input(seconds); // Validate input.
- input_is_valid = true; // The input passed validation.
- }
- catch (...) // Catch any exceptions.
- {
- cout << "Number of seconds is invalid. Be sure to enter an integer.\n";
- }
- }
- total_seconds = (minutes_int * 60) + seconds_int; // Calculate total seconds requested.
- cout << "Timer will run now ...\n";
- run_timer(total_seconds); // Run the timer.
- cout << beep << '\n'; // Make a sound when time is up.
- cout << "Time is up!\n";
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement