0

I working on a quick time event using kbhit function where the user if he presses a specific key does one thing otherwise something else happens. I also wanted to add that if within a time limit the user doesn't click that key the program goes into the else. I wrote this, but it creates a loop. This is the code and if you could help me solve this problem, it would be a great pleasure.

#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");
}
  • `&& sleep == 0` is not calling the function. – Barmar Apr 16 '21 at 17:21
  • 2
    `sleep(3)` pauses the program for 3 seconds. It doesn't tell you if 3 seconds have elapsed. – Barmar Apr 16 '21 at 17:22
  • 2
    If you want to know if 3 seconds have elapsed, set a variable to the time at the start, set another variable to the current time, and test if they differ by at least 3. – Barmar Apr 16 '21 at 17:23
  • 1
    `if((c != 'c' || c != 'C')` should be `if((c != 'c' && c != 'C')`. Or set `c` to `tolower(getch())` so you don't need to compare both cases. – Barmar Apr 16 '21 at 17:24
  • 1
    If you don't understand why that's wrong, see https://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar Apr 16 '21 at 17:24
  • 1
    You could also just use `else`, since the condition is the opposite of the previous condition. – Barmar Apr 16 '21 at 17:25

0 Answers0