C++ 是否有办法在等待用户时继续循环';在不停止循环的情况下输入?

C++ 是否有办法在等待用户时继续循环';在不停止循环的情况下输入?,c++,loops,user-input,C++,Loops,User Input,我正在尝试创建一个每秒钟更新一次的时钟,如果用户按下一个按钮,就会弹出一个菜单,允许他们调整时间 我现在有一个每秒更新一次的时钟,以及调整它的菜单,但是当我将两者结合在一起时,当我等待用户输入内容时,时钟停止更新。有没有办法让时钟继续循环,只有在有任何用户输入时才停止?这是我当前的代码: #include <iostream> #include <string> #include <time.h> #include <unistd.h> #incl

我正在尝试创建一个每秒钟更新一次的时钟,如果用户按下一个按钮,就会弹出一个菜单,允许他们调整时间

我现在有一个每秒更新一次的时钟,以及调整它的菜单,但是当我将两者结合在一起时,当我等待用户输入内容时,时钟停止更新。有没有办法让时钟继续循环,只有在有任何用户输入时才停止?这是我当前的代码:

#include <iostream>
#include <string>
#include <time.h>
#include <unistd.h>
#include <iomanip>


using namespace std;

void ReadUserInputClock(int& addTime) {
    int seconds = 1;
    int minutes = 60;
    int hours = 3600;
    char userInput;
    {   
        cout << " ___________________________" << endl;
        cout << "|Press '1' to add 1 second  |" << endl;
        cout << "|Press '2' to add 2 second  |" << endl;
        cout << "|Press '3' to add 3 second  |" << endl;
        cout << "|Press 'q' to quit          |" << endl;
        cout << " ___________________________" << endl;
        cin >> userInput;
        if (userInput == '1') {
            addTime = seconds;
        }
    
        else if (userInput == '2') {
            addTime = minutes;
        }
    
        else if (userInput == '3') {
            cout << '3';
            addTime = hours;
        }

        else if (userInput == 'q') {
            exit(0);
        }
        
        else {
            addTime = 0;
            cout << "No change." << endl;
        }

    }
}

void DisplayTime (int userTime) {
    char stopProgram = 'g';
    int timeYear;
    int timeMonth;
    int timeDay;
    int timeHour;
    int timeMinute;
    int timeSecond;
    struct tm *tim;
    time_t rawTime = time(NULL) + userTime;
    tim = localtime (&rawTime);
    timeYear = (tim->tm_year + 1900);
    timeMonth = (tim->tm_mon + 1);
    timeDay = tim->tm_mday;
    timeHour = (tim-> tm_hour - 5);
    timeMinute = tim->tm_min;
    timeSecond = tim->tm_sec;
    cout << "                       " << timeYear << "-" << setw(2) << timeMonth << "-" << setw(2) << timeDay << endl;
    cout << "_______________________________________________________" << endl;
    cout << "|     12-Hour Clock     |     |     24-Hour Clock     |" << endl;
    cout << "|                       |     |                       |" << endl;
    
    
    if (timeHour > 12) 
   {
       cout << "|      " << setw(2) << setfill('0') << timeHour - 12 << ":" << setw(2) << timeMinute << ":" << setw(2) << timeSecond << " PM" << "      |";
    }
    else 
   {
        cout << "|      " << setw(2) << setfill('0') << timeHour << ":" << setw(2) << timeMinute << ":" << setw(2) << timeSecond << " AM" << "      |";
   }
    cout << "     " ;
    cout << "|        " << setw(2) << setfill('0') << timeHour << ":" << setw(2) << timeMinute << ":" << setw(2) << timeSecond << "       |" << endl;
    cout << "_______________________________________________________" << endl;
    cout << "                  " << "Press 'm' for menu" << endl;

    cout << endl;
    
    //cout << string(50, '\n');
    }

void userClock() {
    int addTime = 0;
    int tempTime = 0;
    string input;
    while (addTime != -1) 
    {
        while (getline(cin, input)) {
            if (input.empty()) {
                DisplayTime(tempTime);
                sleep(1)
            }
        ReadUserInputClock(addTime);
        tempTime = tempTime + addTime;
        input.clear();
    }
    }
}





int main()
{
   userClock();
   return 0;
}

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void ReadUserInputClock(int和addTime){
整数秒=1;
整数分钟=60;
整小时=3600;
字符用户输入;
{   

所以在一个资源极低的系统上,我会有一个循环(比时钟输出快得多),它只做两件事:检查时钟是否需要更新,检查用户是否提供了输入。使用这个循环(甚至硬件中断!)你可以在看到用户输入时简单地做出反应。记住,这是一个贪婪的解决方案。你应该考虑做一段线程睡眠来节省资源。你知道如何使用<代码> STD::线程< /代码>,<代码> STD::MutXe<代码>,和<代码> STD::条件句变量< /代码>?对不起,如果我误解了,但不是WHA吗?TyMeUsCyLoor()函数是一个带有休眠函数的循环,寻找用户输入的Read UnServEnter()的输入,否则它只运行DePrascLoCK()函数,或者至少它应该是山姆做的,我不不幸,这是我第一次用C++工作,这是我的第一个任务。