如何制作一个可以接受多字符输入的倒数计时器?

时间:2019-12-19 06:32:55

标签: c++ windows chrono

上下文

我制作了一个计时器,从该计时器开始倒计时,如果您输入有效的输入,它将显示其各个元素。如果您在规定的时间内未输入有效的输入,它将结束程序。

问题

问题在于它仅接受单个字符输入。我希望它能够接受多字符输入。但是,当我尝试使用getline时,计时器停止倒计时。

问题

有人知道如何做到这一点,以使计时器接受多个字符输入吗?

    long long int secondsUntilGuardEat = 5; 
    unsigned char pressedKey = NULL; 
    long long int seconds_from_1970 = time(NULL); 

    cout << "Corridor." << endl;
    cout << "You walk down the corridor and at the end you find another corridor with a guard blocking it." << endl;
    cout << "'1' to attack the guard" << endl;
    cout << "'2' to examine the guard" << endl;
    cout << "'3' to go west" << endl;
    cout << "If you do nothing the guard attack you" << endl;

    while (true)
    {
        if (_kbhit()) 
        {
            pressedKey = getchar(); 
            break;
        }
        if (seconds_from_1970 + secondsUntilGuardEat <= time(NULL))break;
        system("cls"); 

        cout << "Corridor." << endl;
        cout << "You walk down the corridor and at the end you find another corridor with a guard blocking it." << endl;
        cout << "'1' to attack the guard" << endl;
        cout << "'2' to examine the guard" << endl;
        cout << "'3' to go west" << endl;
        cout << "If you do nothing the guard attack you" << endl;

        cout << "The guard sees you and charges. You have " << seconds_from_1970 + secondsUntilGuardEat - time(NULL) << " seconds until the guard reaches you" << endl;
        Sleep(1000);
    }
    system("cls"); 
    switch (pressedKey)
    {
    case '1':
        Guard::attackImpossible();

    case '2':

        Guard::examineImpossible();

    case '3':

        cout << "You try to run away but the guard has 1 billion speed and easily catches up with you." << endl;
        Sleep(500);

        cout << "The guard runs up and eats you. You died." << endl;

        Global::gameOver();

    default:

        cout << "You took to long. The guard ran up and ate your face off." << endl;
        Global::gameOver();

    }

0 个答案:

没有答案