So I've got this code here, it's a stopwatch. And the only problem with it is that it doesn't stop. I would like it to stop counting time when I, for example, press the "p" button. Could you help me?
Hm... I’d say you can either use threads or the select function to solve that.
Threads make it possible to have your program do several things at once, so you could use it to have one thread constantly waiting for input, that input being a 'q', and a second thread constantly sending the timer output to that screen.
The select() function tests for a specified amount of time whether or not a specified file descriptor has some info to read. Using this you could instead have a single infinite loop that displays the clock, tests for input for some amount of time, and then displays the clock again.
You can try but I need as simplest explenation as possible. I'm not very good with c++ and my professor wants me to be an expert or he gives a bad grade...
I'm trying my best but it's still too little.
:/ ahh oof. Ok. Ok, so I think I’ll take the thread route, because it’ll be easier for this.
As defined by this site:
A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space.
Most of that is irrelevant jargon. Let’s just focus on two things:
executed concurrently
and
sharing a same address space.
.
The first, basically, "done at the same time", and the second just means in our case that they can share variables and data.
That being said this means I could
Ok that went a bit far. Threads means executing at the same time. That’s all. Sry lol. Ok. Anyways.
To use threads, we must include the thread header #include <thread>
Then to make a thread, we pass a function to the thread constructor. (If you don’t know what that is, don’t worry, it’s not entirely relevant to this atm.) std::thread(function_name);
now you have a thread running that function. So to use your program as an example:
Now how is this helpful? Well, this means that you can do two things at once without them interrupting or blocking each other. This means you can check for input and display output at the same time!
Your turn!
Steps:
1 2
1) make thread running func
2) cin a character. Is it q? Exit!
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
Ok, so my first question is why I have this error when I try to run a code with thread:
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
And a second one, where I should put the first thread? maybe the place choosen by me isn't right, will putting just std::thread is enough to stop the program? or how to include this "q", what does that even mean?
ok, more than one question...
....oh. Dam. Um. So that means that your compiler probably doesn’t support threads because it’s a c++11 feature. This is strange. What compiler are you using? Can you update it...?
std::thread(timer) makes a thread running the timer function. It does not stop the program. That example program just runs forever. Sorry about the q, I was just talking about the order in which an example program could go. Like
1 2 3 4 5
char c;
std::thread(timer); // make thread
cin >> c; // cin a character
if(c == 'q') // is the character a 'q'?
exit(0); // exit!
I'm using dev c++, don't know if it can be updated.
okay, so what does stop the program?
Thank you for your patient, I'm literally crying my eyes out for four days trying to make it work and it doesn't.
The program stops because the main thread (literally the main function in this case) either returns or exits. Either way that signals the end of the entire process, so all threads are destroyed.
Here’s a little example:
I'm using dev c++, don't know if it can be updated.
It can't be *updated. You should consider getting a newer IDE able to work with the newer language standards. Visual Studio or Code::Blocks. Dev-C++ is no longer actively supported.
MSVC has a free version available, and C::B is also free.
Okey, so I gave up with trying to make it stop, but I saw that it doesn't count time properly, it's slower. I was trying to fix it but I failed. Could someone modify this code to make it right? :( (I think the problem starts after void timer)
#include <ctime>
#include <unistd.h>
#include <iostream>
#include <string>
void timer(clock_t start_time);
void display_clock(int hrs, int mins, int secs);
int main() {
timer(clock());
}
void timer(clock_t start_time) {
while(1) {
sleep(1); // instead of using this to base your calls off of, just use it as a pausing point. A time of rest. No calcs should be based off it.
clock_t time_elapsed = clock() - start_time;
int hrs, mins, secs;
secs = /* calc the amnt of seconds elapsed using time_elapsed */;
mins = /* calc mins using secs */;
hrs = /* calc hrs using mins */;
display_clock(hrs, mins, secs);
}
}
void display_clock(int hrs, int mins, int secs) {
// erase stuff written on this line
std::cout << std::string('\b',300) << std::flush;
std::cout << "hrs: " << hrs << ", mins: " << mins << ", secs: " << secs;
std::cout << std::flush;
}
main.cpp:63:13: error: ‘this_thread’ has not been declared
this_thread::sleep_for(chrono::seconds(1));
^~~~~~~~~~~
main.cpp:65:20: error: ‘_kbhit’ was not declared in this scope
if( _kbhit() ) sign = _getch();