i'm using while loop per below:
do { scanf("%c", &turnchoice); if (turnchoice == 'r') { invalidselection = false; } else if (turnchoice == 'h') { invalidselection = false; } else { printf("invalid input.\n"); } } while (invalidselection == true);
however, before stopping user input, runs full loop once (so displays "invalid input" , asks user letter). doing wrong?
my guess have input before code show, input end newline, right?
the problem here newline still in input-buffer when call scanf
read character, newline read , get.
there simple "trick" tell scanf
read and discard leading white-space (like newlines), , prepend single space format string, try e.g.
scanf(" %c", &turnchoice); // ^ // | // note space here
Comments
Post a Comment