[英]can countdown work simultaneously with the program in c++
I am currently making a quiz program. 我目前正在制作测验程序。 And I created a countdown, but as c++ does actions line by line I am getting what I want. 我创建了一个倒数计时,但是随着c ++逐行执行动作,我得到了想要的东西。 I want countdown work simultaneously with the tests. 我希望倒数计时与测试同时进行。
here is the one part of my program 这是我程序的一部分
void QUIZ::OOP2()
{
system("cls");
QUIZ("OOP2");
int oop2_time = 100;
for (int i = oop2_time; oop2_time >= 0; i--)
{
cout << "\t\t\tQuestions of OOP2\n\n";
line();
cout << "1. Switch selection structure can be used to represent any kind of if-else selection structure? \n";
line();
cout << " a) True b) False \n";
CorrectB(var);
if (oop2_time == 100)
{
cout << "You have the remaining" << oop2_time << "seconds \n" << endl;
Sleep(40000);
}
if (oop2_time == 60)
{
cout << "You have the remaining" << oop2_time << "seconds \n" << endl;
Sleep(30000);
}
if (oop2_time == 30)
{
cout << "You have the remaining" << oop2_time << "seconds \n" << endl;
Sleep(15000);
}
if (oop2_time == 15)
{
cout << "You have the remaining" << oop2_time << "seconds \n" << endl;
Sleep(10000);
}
if (oop2_time == 5)
{
cout << "You have the remaining" << oop2_time << "seconds \n" << endl;
Sleep(5000);
cout << "\nTime is out\n";
goback();
intro();
}
result("OOP2");
goback();
}
}
Without using multi-threading or std::chrono but just your base code, here's a code snippet. 在不使用多线程或std :: chrono的情况下,仅使用您的基本代码,这是一个代码段。 I have used recursive function and a time resolution of 1 second. 我使用了递归函数和1秒的时间分辨率。 The countdown can also be terminated prematurely in the main by setting stop = true;
通过设置stop = true;
还可以提前终止倒数计时stop = true;
: :
#include <iostream>
#include <windows.h>
void wait(double sec, bool& stop)
{
std::cout <<"Time remaining "<< sec << " sec\n";
if (stop || sec <= 0) return;
Sleep(1000);
wait(sec-1, stop);
}
int main()
{
bool stop = false;
wait(5, stop);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.