首页 > 解决方案 > C - if 中的睡眠功能

问题描述

我使用 kbhit 函数处理一个快速时间事件,其中用户如果按下特定键会做一件事,否则会发生其他事情。我还想补充一点,如果用户在一段时间内没有点击那个键,程序就会进入 else。我写了这个,但它创建了一个循环。这是代码,如果你能帮我解决这个问题,那将是非常高兴的。

#include <stdio.h>  
#include <stdlib.h>  
#include <conio.h>  
#include <time.h>  
#include <unistd.h>  

int main()
{

    printf("\nYou approach him, you try to take him by the legs from behind.\n");
    Sleep(1500);
    
    
    while (!kbhit())
    {

        printf("PRESS C !!!\n");
        int c = getch();
        if ((c == 'c' || c == 'C') && sleep(3) != 0)
        {
            printf("Can you pick him up by the paws and\n");
            break;
        }
        else if((c != 'c' || c != 'C') && sleep == 0)         //damage 40
        {
            printf("\nYou failed to catch it and as it turns around it hurts you with its tail.\n");
            getch();
            break;
        }
    }
    printf("Jhon takes out his Swiss Army knife and stabs him in the throat, killing him.\n");
}

标签: c

解决方案


推荐阅读