Added loop and exit condition

TODO: Segmentations fault is thrown upon pressing newline or adding spaces
Fixed the newline case