Rewrite numbers of at most 2 digits of the input to the output

-2

I need to rewrite 2-digit input numbers into the output , stopping processing input after reading the number 42.

Input:

1
2
88
42
99

Output:

1
2
88

My code is like this, I can not understand what's wrong:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int a=100;
    int *n=(int*)malloc(100*sizeof(int));

    for(int i=0;i<100;i++){
        scanf("%d", &n[i]);
        if(n[i]==42){
            a=i++;
            break;
        } 
    }

    for(int i=0;i<a;i++){
        printf("%d\n", n[i]);
    }

    return 0;
}
    
asked by anonymous 03.01.2018 / 14:51

1 answer

0

If you stop reading after reading the number 42, then your repeat should not have a for(int i=0;i<100;i++){ limit but an infinite loop, should be something like: for(int i=0;i>-1;i++){

and your code would look like this

#include <stdio.h>
#include <stdlib.h>

int main(){
    int a=0;
    int *n=(int*)malloc(100*sizeof(int));

    for(int i=0;i>-1;i++){
        scanf("%d", &n[i]);
        if(n[i]==42){
            a=i++;
            break;
        } 
    }

    for(int i=0;i<a;i++){
        printf("%d\n", n[i]);
    }

    return 0;
}

If this is the error.

    
03.01.2018 / 15:01