简体   繁体   English

可以与C ++中的程序同时倒计时

[英]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.

相关问题 如何在使用 c++ 创建的测验程序中插入倒数计时器? - How to insert a countdown timer in a quiz program that is created using c++? valgrind / callgrind可以在发布的可执行C ++程序上工作吗? - Can valgrind/callgrind work on a release executable C++ program? C ++同时控制另一个程序的I / O - C++ control I/O of another program simultaneously 如何让 C++ 程序同时做两件事? - How to make C++ program doing 2 things simultaneously? 如何倒计时到0 C++ - How to countdown to 0 C++ 您能否在不使用 GetAsyncKeyState 或线程的情况下为 windows 程序在 C++ 中运行 function 的同时获得用户输入? - Can you simultaneously get user input while also running a function in C++ for a windows program without using GetAsyncKeyState or threading? 如何使用C ++中的以下代码生成秒表​​/倒计时? - How can I generate a stopwatch/countdown with the following code in C++? IO:同时从C ++程序和另一个Java程序写入和读取相同的文本文件吗? - IO : Writing and reading to the same text file from a C++ program and from another Java program simultaneously? 如何在C ++中同时子类化几个类? - How can I simultaneously subclass several classes in C++? 为什么 C++ 不能同时释放内存和指针 - why C++ can't free memory and pointer simultaneously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM