Ask coding questions

← Back to all posts
Timer cpp
darklordplati

I need to run a timer while the program is la running something else. When the timer stops, the program will stop as well

My code is showing errors.
int random() {
using namespace std;
int answer;
cout << iSecret;
chrono::steady_clock::time_point tend=chrono::steady_clock::now() + chrono::seconds(10);
while (tend < 10) {
cin >> answer;
if (answer == tend) {
money = money + 10;
} else {
money = money - 10;
}
cout << "you have ";
cout << money;
cout << "cash";
}
}

Voters
wyattowens2023
darklordplati
Comments
hotnewtop
CPPStudent7308

If you use the file <unistd.h>, and use the command sleep(number of seconds); , you can make the program stop for a certain number of seconds. This works in C++, which looks like what you could be using. Let me know if this answers your question.

darklordplati

I need the timer to run while the program is running. When the timer stops, then the whole thing will [email protected]

CPPStudent7308

@darklordplati Okay, I see. What you need is a break command that tells the computer to go back to the main function when a certain function is done running. You can implement this at the end of your timer function so that when it breaks to main, there is nothing left in main to run, so the program stops. Does that make sense? (p.s. I do not know if this is cross platform. All I know is that it works in C++ at least)