0
#include<stdio.h>

int main(){
    
    int T, N, K;
    
    scanf("%d", &T);
    
    for(int i = 1; i <= T; i++){
        scanf("%d %d", &N, &K); getchar();
        char hall;
        int hold = 0;
        for(int j = 0; j < N; j++){
            scanf("%c", &hall); getchar();
            printf("Case #%d:", i);
            if(hall == '0'){
                hold++;
            } else{
                hold = 0;
            }
            if(hold >= K){
                printf("Dead\n");
                return 0;
            }
        }
        printf("Alive\n");
        return 0;
    }
    
    return 0;
}

I got a case where i need to make a program that we must know the outcome of a man who trapped inside a building that caught on fire, we have to make a program where we know he ended up Dead or Alive. This problem includes a testcase (int T), length of a hall (int N), and the duration of the man's capability to hold his breath (int K). In the int N (hall) will contain '0' and '1' where each of it indicates there's stair (1) and there's no stair (0); if there's stair that means the man can take a breath.

Example input:

2 -> this is how many testcase

8 2 -> int N and int K, case 1

01010100 -> int N numbers

10 3 -> case 2

0001001110

The output should be:

Case #1: Dead

Case #2: Alive

Code above already works, but i wonder where should i put the printf("Case #%d:", i); because if i put it there, the if wont run so it wont print either Dead or Alive. Does anyone have an idea?

  • What you should do is to flush the buffer after printing. See the dupes. – klutt Nov 15 '22 at 13:05
  • @klutt it makes the if wont run – waterswell Nov 15 '22 at 13:06
  • It certainly does not, but I have a strong feeling that the other dupe about leaving newlines in input buffer is the problem. – klutt Nov 15 '22 at 13:10
  • Oh, wait. You had a getchar there. Missed that. – klutt Nov 15 '22 at 13:11
  • Nevertheless, there's absolutely no way a `fflush(stdout)` could make an if statement not run – klutt Nov 15 '22 at 13:12
  • @klutt you can copy my code and see it really doesnt solve the problem, fflush(stdin) makes the if wont run and fflush(stdout) makes my printf loop (which means the if wont run) its the same – waterswell Nov 15 '22 at 13:16
  • Yes, I never said you should flush stdin. But if you enter `01010100` the `getchar()` will consume every other character. – klutt Nov 15 '22 at 13:18
  • And I strongly recommend [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and possibly also [how to debug small c programs](https://github.com/klutt/debug-small-c-programs) (The latter is written by me) – klutt Nov 15 '22 at 13:21
  • @klutt ok so i delete the getchar for hall, the if statement alr run but the printf got loop instead – waterswell Nov 15 '22 at 13:23
  • Np. Good luck. And btw, `scanf` is notorious for causing troubles. Here is a question about what to use instead. https://stackoverflow.com/q/58403537/6699433 – klutt Nov 15 '22 at 13:25

0 Answers0