C++ 如何在C++;在给定时间内获取用户输入?

C++ 如何在C++;在给定时间内获取用户输入?,c++,timer,user-input,C++,Timer,User Input,我需要用户输入在一定时间范围内按下一个键。如果他不按任何键,则秒变量递增。下面您可以找到流程图和我的代码 int DisplayMenu()//此函数显示用户菜单。 { int userChoice{}; cout您可以计算用户输入字符所需的秒数: int DisplayMenu() //This function displays the user menu. { int userChoice{}; cout << "\t\t***************

我需要用户输入在一定时间范围内按下一个键。如果他不按任何键,则秒变量递增。下面您可以找到流程图和我的代码

int DisplayMenu()//此函数显示用户菜单。
{
int userChoice{};

cout您可以计算用户输入字符所需的秒数:

int DisplayMenu() //This function displays the user menu.
{
    int userChoice{};
    cout << "\t\t***************************" << endl;
    cout << "\t\t* 1-Add One Hour          *" << endl;
    cout << "\t\t* 2-Add One Minute        *" << endl;
    cout << "\t\t* 3-Add One Second        *" << endl;
    cout << "\t\t* 4-Exit Program          *" << endl;
    cout << "\t\t***************************" << endl;
    std::chrono::timepoint before = std::chrono::high_resolution_clock::now();
    cin >> userChoice;
    std::chrono::timepoint after = std::chrono::high_resolution_clock::now();
    seconds += std::chrono::floor<std::chrono::seconds>(after - before).count();
    return userChoice; //The value returned will be use as argument for the UserChoice function.
}

或者,您可以在单独的线程中运行递增的
秒数。

您可以计算用户输入字符所需的秒数:

int DisplayMenu() //This function displays the user menu.
{
    int userChoice{};
    cout << "\t\t***************************" << endl;
    cout << "\t\t* 1-Add One Hour          *" << endl;
    cout << "\t\t* 2-Add One Minute        *" << endl;
    cout << "\t\t* 3-Add One Second        *" << endl;
    cout << "\t\t* 4-Exit Program          *" << endl;
    cout << "\t\t***************************" << endl;
    std::chrono::timepoint before = std::chrono::high_resolution_clock::now();
    cin >> userChoice;
    std::chrono::timepoint after = std::chrono::high_resolution_clock::now();
    seconds += std::chrono::floor<std::chrono::seconds>(after - before).count();
    return userChoice; //The value returned will be use as argument for the UserChoice function.
}
或者您可以在单独的线程中运行
seconds
递增