Redirect your output to a file rather than the screen, thats the best way.
PS - when I said doesn't handle anything after 2 digit numbers, I mean:
12
34
first you read num[0]=1, num[1]=2, then you read num[0]=newline, num[1]=3, then you read num[0]=4 and num[1]=newline, so you don't get the correct numbers.