如何允许计时器在后台继续运行
How to allow timer to keep running in the background
我正在为我的俱乐部编写一个程序。要求用户在 2 分钟内回答 10 个问题。我为每个问题使用函数,并在用户回答完当前问题时调用下一个函数。如何在显示问题的同时在顶部显示剩余时间?
我尝试过循环来控制我的时间。但它不会在顶部显示剩余时间并在循环需要执行的同时显示问题,直到条件为 false。
Question1()
{
countdown();
cout<<"Question 1 out of 10"<<endl<<endl;
cout<<" Where Is The First Indoor Bowling Lane Built? "<<endl;
cout<<"A. New York City"<<endl;
cout<<"B. Berlin"<<endl;
cout<<"C. Ohio"<<endl;
cout<<"D. Japan"<<endl;
cout<<"Your answer: ";
cin>>ans1;
Question2();
}
void countdown()
{
while(timer>=0)
{
cout<<"Time remaining: "<<timer<<endl;
Sleep(1000);
timer--;
system("cls");
}
}
预计时间在我的问题之上倒计时,但问题只会在设置的时间结束之前显示。
我回想起MS-DOS的时代,记得Windows中<conio.h>
_kbhit
提供了一个功能。 它不是便携式的,但也许你不在乎。 这是我敲出的一个函数,它将使用_kbhit
轮询按键。 它像胆量一样粗糙,但也许这就是你要找的。
int GetInputWithTimeout(int count, int timeout)
{
int answer = -1;
const int poll_rate = 10;
int poll_cycle = 0, last_timeout = 0;
do {
// poll for keypress and store answer if valid
if (_kbhit())
{
int input = _getch();
while (_kbhit()) _getch(); // discard control sequences
input = tolower(input);
if (input >= 'a' && input < 'a' + count)
{
answer = input - 'a';
}
}
// display time remaining if changed
if (last_timeout != timeout) {
last_timeout = timeout;
std::cout << "rTime remaining: " << timeout << " " << std::flush;
}
// perform small sleeps for a potentially wildly inaccurate, but responsive delay
if (timeout > 0)
{
Sleep(1000 / poll_rate);
poll_cycle = (poll_cycle + 1) % poll_rate;
if (poll_cycle == 0) --timeout;
}
} while (timeout > 0 && answer == -1);
std::cout << std::endl;
return answer;
}
因此,您可以使用此功能,传递所需数量的测验输入。 建议使用如下:
struct Question {
std::string question;
std::vector<std::string> answers;
int correct_answer;
int timeout_seconds = 10;
};
bool AskQuestion(int number, const Question& question)
{
std::cout << "Question " << number << ": " << question.question << std::endl;
int count = static_cast<int>(question.answers.size());
for (int i = 0; i < count; i++)
{
std::cout << " " << char('A' + i) << ": " << question.answers[i] << std::endl;
}
int answer = GetInputWithTimeout(count, question.timeout_seconds);
return answer == question.correct_answer;
}
您会注意到我在结构中表示了一个测验问题。 因此,您可以像这样设置和运行整个测验:
int main()
{
std::vector<Question> questions = {
{
"Where Is The First Indoor Bowling Lane Built?",
{
"New York City",
"Berlin",
"Ohio",
"Japan"
},
0, // I have no idea, so just guessed it's new york
},
{
"What is the airspeed velocity of an unladen swallow?",
{
"10km/h",
"20km/h",
"30km/h",
"40km/h",
"What do you mean -- an african or european swallow?"
},
4,
},
};
int num_questions = static_cast<int>(questions.size());
int num_correct = 0;
for (int q = 0; q < num_questions; q++)
{
num_correct += AskQuestion(q, questions[q]);
std::cout << std::endl;
}
std::cout << "Final score: " << num_correct << " of " << num_questions << std::endl;
return 0;
}
哎呀,我有点为你写了整个程序。 它并不漂亮,但希望至少能教你一些东西。
相关文章:
- 为什么我的Qt程序在断言失败后继续运行?
- 继续运行程序而无需任何干预,直到要求退出为止
- Qt在特定代码后没有继续运行指令
- 阻止进程在从服务启动时启动,并在某些进程关闭后继续运行服务。
- 测试回文时程序不会继续运行 (C++)
- 如何允许计时器在后台继续运行
- 在SIGABRT c ++信号之后继续运行程序
- 在 fork() 之后,如何在 for() 循环中继续运行 execve()
- 我的程序在调用"parsePacket()"后是继续运行,还是等到收到数据?
- C++循环在 EOF 之后继续运行时这样做
- 在父母获得杀死信号和退出后,用system()产生的儿童进程继续运行
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- if (user == 'y'){} 即使它不等于 'y' 也继续运行
- 我如何接受"standard input commands",然后知道何时继续运行该程序?
- FindFirstFile在加密时继续运行相同的文件
- 创建了三个线程来实例化同一个类.但很快只有一个线程继续运行
- MS Visual C++2010学习版继续运行旧代码
- 程序在退出(0)后似乎继续运行
- 程序继续运行,没有任何输出
- 获得由内存泄漏引起的内存溢出,应用程序继续运行和分配