While loop to retrieve input, not working correct in C -
While loop to retrieve input, not working correct in C -
i have problem when using user input method allow validates input. require homecoming input after been validated.
char* getvalidinputnumber(int length, char prompt[],int base) { char* user_input = calloc(length+1,sizeof(char)); fflush(stdin); fflush(file *); /*prompts & gets users input , saves in user_input*/ { printf("\n %s", prompt); fgets(user_input,length+1,stdin); /*printf("\n##entered %s : ", user_input);*/ } while(!isnumeric(user_input,base) && strlen(user_input) != length); fflush(stdin); return(user_input); }
when calling function within main like....
while (strcmp(user_input,"00000000") != 0) { user_input = getvalidinputnumber(8, "enter hex value",16); }
it next ...
enter hex value
enter hex value
twice rather 1 time , when come in hex value returns hex right runs twice 1 time again ive tryed using fflush doesnt seem solve it.
how solve or there improve way input illustration using scanf?
fflush(stdin)
causes undefined behavior! fflush()
should used on streams open output, not input.
c input while-loop hex
Comments
Post a Comment