c - Instruction executes twice -
this question has answer here:
i have following piece of code. want user enter a or r , continue executing program, or try user input if enters else.
but problem is, when enter illegal value program shows message twice, , not once. how can make message ever show once?
label: puts ("a/r?"); c = getchar (); if (c != 'a' || c != 'r') goto label;
if user enters a, have press enter key on keyboard input register. key leaves newline character in standard input , therefore character read second time around , test fails again.
the test fails input because trying have input 2 things @ same time. have use && instead of || in order check if input either 1 of characters looking for. know sounds should other way around, not case have observed.
Comments
Post a Comment