I am facing issues in creating a pause and resumable timer. I have pauseResume function to pause the execution of the code when I give certain input from keyboard and resume when I take another input from the keyboard. The timer has to run continuously until I pause it. It is not working, can someone help me with this? How do I take input and control the loop?
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <thread>
#include <chrono>
using namespace std;
using std::copy;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;
// hours, minutes, seconds of timer
int hours = 0;
int minutes = 0;
int seconds = 0;
int pauseResume()
{
int flag;
cout << "Program is paused !" << std::endl
<< "Press Enter to continue" << std::endl;
// pause the program until user input
flag = getc(stdin);
cout << "\nContinuing .";
sleep_for(300ms);
cout << "...";
cout << "\nProgram finished with success code!" << std::endl;
return EXIT_SUCCESS;
}
// function to display the timer
void displayClock()
{
// system call to clear the screen
system("clear");
cout << setfill(' ') << setw(55) << " TIMER \n";
cout << setfill(' ') << setw(55) << " --------------------------\n";
cout << setfill(' ') << setw(29);
cout << "| " << setfill('0') << setw(2) << hours << " hrs | ";
cout << setfill('0') << setw(2) << minutes << " min | ";
cout << setfill('0') << setw(2) << seconds << " sec |" << endl;
cout << setfill(' ') << setw(55) << " --------------------------\n";
}
void timer()
{
// char operation;
// std::cout << "Enter operation: ";
// std::cin >> operation;
// if(operation =='p')
// // {
// do
// {
// bool startTimer = true;
// bool pauseTimer = false;
while (true)
// while()
{
// display the timer
displayClock();
// sleep system call to sleep for 1 second
sleep(1);
// increment seconds
seconds++;
if (seconds == 10)
{
// // increment minutes
// minutes++;
// // if minutes reaches 60
// if (minutes == 60)
// {
// // increment hours
// hours++;
// minutes = 0;
// }
// seconds = 0;
break;
}
// char pausePlay;
// std::cout << "Enter p to pause";
// std::cin >> pausePlay;
// if(pausePlay == 'p')
// {
// pauseResume();
// }
}
char pausePlay;
std::cin >> pausePlay;
if (pausePlay == 'p')
{
pauseResume();
}
// }while(operation != 'e');
}
// }
// Driver Code
int main()
{
// start timer from 00:00:00
timer();
return 0;
}