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

问题描述 投票:0回答:1

Context

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

问题

问题是它仅接受单个字符输入。我希望它能够接受多字符输入。当我尝试使用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();

    }
c++ windows chrono
1个回答
0
投票

您可以使用语法getline(cin, ...);

© www.soinside.com 2019 - 2024. All rights reserved.