Skip to content

Instantly share code, notes, and snippets.

@dotcomboom
Created June 13, 2021 20:10
Show Gist options
  • Save dotcomboom/30161c455483c0be5702b8d8104316f0 to your computer and use it in GitHub Desktop.
Save dotcomboom/30161c455483c0be5702b8d8104316f0 to your computer and use it in GitHub Desktop.
Pomodoro timer
#include <sstream>
#include <iostream>
#include "windows.h."
using namespace std;
struct timer {
int minutes;
int seconds;
bool active;
string s;
} pomo;
timer tick(timer t) {
t.seconds -= 1;
if (t.seconds < 0)
{
t.seconds = 59;
t.minutes -= 1;
}
if (t.minutes == 0 && t.seconds == 0)
t.active = false;
stringstream ss;
if (t.minutes > 0) ss << t.minutes << " minute";
if (t.minutes > 1) ss << "s";
if (t.minutes > 0) ss << " ";
if (t.seconds != 0) ss << t.seconds << " second";
if (t.seconds != 0 && t.seconds != 1) ss << "s";
t.s = ss.str();
return t;
}
bool check_choice(char c) {
if (c == 'p' || c == 's' || c == 'l') return true;
/*char valid [3] = { 'p', 's', 'l' };
for (int i = 0; i < 2; ++i) {
if (valid[i] == c) {
return true;
}
}*/
return false;
}
int main()
{
cout << "Choose:" << endl;
cout << "[P]omodoro [S]hort Break [L]ong Break" << endl;
char choice;
choice = '_';
while (check_choice(choice) == false) {
cin.get(choice);
choice = tolower(choice);
}
if (choice == 'p') {
pomo.minutes = 25;
} else if (choice == 's') {
pomo.minutes = 5;
} else if (choice == 'l') {
pomo.minutes = 10;
}
pomo.seconds = 1;
tick(pomo);
pomo.active = true;
while (pomo.active) {
if (pomo.seconds == 0 || pomo.seconds == 30) cout << endl << pomo.s;
Sleep(1000); // This uses the Windows api
pomo = tick(pomo);
}
cout << endl << "Timer end!\a";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment