Input and output being on the same terminal window is definitely the root of the problem, you're bound to run into I/O stream collisions doing it like this. I would look into using ncurses to split your terminal view into two windows possibly an input window at the bottom and an output window at the top, that choice is left up to you :P