Write a program that runs a counter in a separate thread. The counter should sta
ID: 3575728 • Letter: W
Question
Write a program that runs a counter in a separate thread. The counter should start at one and increment its value every second. Every five seconds the counter should output its value to the console. The following line of C++11 code will make the current thread wait for one second: std::this_thread::sleep_for(std::chrono::seconds(1)); While the counter thread is running your main thread should allow you to input a number. If the number entered is less than or equal to the counter's value then the program should stop.
Explanation / Answer
#include<iostream>
#inlcude<ctime>
using namespace std;
class calculate{
void counter()
{
while(sleep(5))
{
int i=0;
i=i+5;
cout<<i;
wait(1);
call( i );
}
void call(int value){
int temp = value;
int num;
cout<<"enter a number";
cin>>num;
if(num < = temp)
{
cout<<"program can't be run";
exit(1);
}
else{
counter();
}
}
int main(){
calculate obj = new calculate();
obj.counter();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.