蜂鸣声,直到任何输入 [英] beep sound till any input

查看:192
本文介绍了蜂鸣声,直到任何输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个猜谜节目。所以,我要的是当用户之前psented在$ P $任何问题的话,他有30秒时间来回答。而在这些30秒我想以1秒的间隔的蜂鸣声('\\一个')。现在我想的是,这蜂鸣声应尽快用户输入任何输入停止。我创建了这个小功能产生蜂鸣声,持续30秒无效音(){的for(int i = 0; I< 30;我++){COUT<<\\ A;睡眠(1000); }
}

但我不知道在用户输入他/她的回答如何尽快停止,因为一旦我把它叫做什么可以直到它在做。
谁能给什么解决办法呢?

I am making a Quiz program. So what I want is whenever any question in presented before the user, then he has 30 seconds to answer it. And in these 30 seconds I want the beep sound ('\a') at an interval of 1 second. Now I want is that this beep sound should stop as soon as the user enters any input. I have created this small function to produce the beep sound for 30 sec void beep(){ for(int i=0;i<30;i++){cout<<"\a"; Sleep(1000); } } But I don't know how to stop it as soon as the user enters his/her answer because once I call it nothing can be done until its over. Can anyone give any workaround for it?

推荐答案

免责声明:我不是一个Windows程序员,我不知道这是好作风或者即使它会编译或工作。我不能在这里进行测试。然而,由于没有其他人给出的解决方案,这是一个起点。因为我了解我将修改这个答案,并希望有人谁知道更多关于这将露面。

Disclaimer: I'm not a Windows programmer, I don't know if this is good style or even if it will compile or work. I can't test it here. However, as no one else has given a solution, it's a starting point. I'll edit this answer as I learn more, and hopefully someone who knows more about this will turn up.

修改:我伪造了 _kbhit()来一个简单的函数返回 ,它至少编译和看起来像它运行正常。

Edit: I faked out _kbhit() to a trivial function returning false, and it at least compiles and looks like it runs ok

修改:好吧,我确实有MS在工作Visual Studio中,我从来没有使用它。在code,因为它是现在编译和作品(我怀疑时间结束了,虽然)。

Edit: Ok I do have ms visual studio at work, I just never use it. The code as it is right now compiles and works (I suspect the timing is off though).

修改:已更新其立即读回被打(而不是等待用户按下回车键)键

Edit: Updated it to immediately read back the key that was hit (rather than waiting for the user to hit enter).

这是最重要的功能: HTTP: //msdn.microsoft.com/en-us/library/58w7c94c%28v=vs.80%29.aspx

#include <windows.h>
#include <conio.h>
#include <ctime>
#include <iostream>
#include <string>

int main()
{
    time_t startTime, lastBeep, curTime;
    time(&startTime);
    lastBeep = curTime = startTime;
    char input = '\0';

    while ( difftime(curTime,startTime) < 30.0 )
    {
        if ( _kbhit() ) // If there is input, get it and stop.
        {
            input = _getch();
            break;
        }
        time(&curTime);
        if ( difftime(curTime,lastBeep) > 1.0 ) // More than a second since last beep?
        {
            std::cout << "\a" << "second\n" << std::flush;
            lastBeep = curTime; // Set last beep to now.
        }
    }
    if ( input )
    {
        std::cout << "You hit: \"" << input << "\"\n" << std::flush;
    }

    return 0;
}

这篇关于蜂鸣声,直到任何输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆