Nested Conditional Operators in C -
this question has answer here:
- scanf not taking in data 3 answers
i having little trouble working nested conditional operators in c.
int is_correct() { char yn ; printf( "y or n : " ) ; scanf( "%c", &yn ) ; yn = toupper( yn ) ; return ( yn == 'y' )? 1 : ( yn == 'n' )? 0 : is_correct() ; }
i under impression last line of code return 1 or 0 if 'y' or 'n' entered or call again if unexpected character entered. instead continuously calls no matter input.
probably scanning failing, , you're not verifying it.
you don't specify if "continuously" means "without stopping read more input", of course should do.
note, instance, toupper()
uses int
-typed argument , results, , expecting values of type unsigned char
might hit undefined behavior there.
this confusing aspect of ctype.h
functions. tend cast unsigned char
in call, if data comes text (char
) buffer.
add printf()
call print out value of yn
before final line.
Comments
Post a Comment