How can i make my timer run while I can input? Help please.
Mar 26, 2015 at 9:27pm UTC
Like if I hit Escape button from my keyboard, the loop will stop?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
int main()
{
char ans;
int m, s,h;
cout << "A COUNTDOWN TIMER " << endl;
cout << "enter time in hours here" << endl;
cin >> h;
cout << "enter time in minutes here " << endl;
cin >> m;
cout << "enter im in seconds here" << endl;
cin >> s;
cout << "Press any key to start" << endl;
cout << " A COUNTDOWN TIMER" << endl;
cout << "time remaining" << endl;
cout << "hours : " << h << "mins : " << m << " secs : " << s << endl;
for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0 ; min--)
{
if ( min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if ( sec == 0 )
s = 59;
Sleep(1000);
system("cls" );
cout<<"\n\n[Esc] Logout" <<endl;
cout << hour << " :hours " << min << " :mins " << sec << " :secs" << endl;
cin>>ans;
switch (ans){
case 'a' :
break ;
}
}
}
}
Sleep(1000);
cout << "THE END" << endl;
return 0;
}
Last edited on Mar 26, 2015 at 10:15pm UTC
Mar 26, 2015 at 11:03pm UTC
You'll have to work with a thread here...
in your main you just print everything and in the thread you check if the key is pressed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <iostream> // std::cout, std::cin
#include <windows.h>
using namespace std;
std::atomic<bool > a_set = false ;
void check()
{
char ans;
while (a_set == false )
{
cin >> ans;
switch (ans)
{
case 'a' : a_set = true ; break ;
default : break ;
}
}
}
int main()
{
std::thread keybord_checker(check);
char ans;
int m, s,h;
cout << "A COUNTDOWN TIMER " << endl;
cout << "enter time in hours here" << endl;
cin >> h;
cout << "enter time in minutes here " << endl;
cin >> m;
cout << "enter im in seconds here" << endl;
cin >> s;
cout << "Press any key to start" << endl;
cout << " A COUNTDOWN TIMER" << endl;
cout << "time remaining" << endl;
cout << "hours : " << h << "mins : " << m << " secs : " << s << endl;
for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0 ; min--)
{
if ( min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if ( sec == 0 )
s = 59;
Sleep(1000);
system("cls" );
cout<<"\n\n[Esc] Logout" <<endl;
cout << hour << " :hours " << min << " :mins " << sec << " :secs" << endl;
if (a_set == true )
break ;
}
if (a_set == true )
break ;
}
if (a_set == true )
break ;
}
Sleep(1000);
cout << "THE END" << endl;
cout << "still waiting for input" << std::endl;
keyboard_checker.join();
return 0;
}
Mar 26, 2015 at 11:13pm UTC
Is the code that you posted working? Compiled it and got tons of error. I think it's because of my compiler?
I haven't been into threads before! I think I need some help X_X
Last edited on Mar 26, 2015 at 11:15pm UTC
Mar 27, 2015 at 1:27pm UTC
Yeah it's working but you need some ... libraries and flags
If you are using g++ and the commandline to compile you could do it like this:
g++ -std=c++11 -pthread main.cpp
Topic archived. No new replies allowed.