getaline() could return -1 so make the return type an int. for() to while() because n is not the for()'s iteration variable.