Change:
user_opt = getchar();
to
user_opt = getchar();
while( user_opt != '\n' && getchar() != '\n' )
{
// do nothing
}
This will remove any characters that follow that assigned to user_opt
from teh line buffer - including the inevitable '\n'.
The console on most systems is line buffered, so getchar()
will not return until an entire line is buffered (i.e. you have pressed ENTER); then you have at least two characters buffered = the one you entered and the newline from pressing ENTER. If you don't remove all buffered characters, getchar()
will return immediately on a subsequent call. The loop continues to get characters until it finds a newline, but checks user_opt
in case the user simply pressed ENTER.