Clean Code hochulshin.com

C++ - How to figure out the unknown number of inputs

2015-10-28

Simple example

#include <stdio.h> 

char input[1000];
int Arr[10];

int main(){
    int n;
    fgets(input, 1000, stdin);
    sscanf(input, "%d", &n);
    fgets(input, 1000, stdin);
    int index = 0;
    int a_i = 0;    
    int pt = 0;
    while(input[index] != 0){
        if(input[index] == ' ' || input[index] ==10){
            input[index] = 0;
            sscanf(input + pt, "%d", &Arr[a_i++] );
            pt = index+1;
        } 
        index++;
    }
    for(int i = 0; i<a_i;i++){
        printf("%d ", Arr[i]);
    }
    return 0;
}

Comments