I have a problem when using a user input method which allow validates the input.
I require to return the input after its been validated.
char* getvalidinputnumber(int length, char prompt[],int base)
{
char* user_input = calloc(length+1,sizeof(char));
fflush(stdin);
fflush(FILE *);
/*Prompts & Gets the users input and saves it in user_input*/
do {
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 this function within my main like….
while (strcmp(user_input,"00000000") != 0)
{
user_input = getvalidinputnumber(8, "Enter HEX Value",16);
}
It also does the following …
Enter HEX Value
Enter HEX Value
Twice rather than just once and when i enter a hex value it returns the hex correct but then runs twice again ive tryed using fflush but this doesnt seem to solve it.
How could i solve this or is there a better way to get the input for example using scanf?
Causes an Undefined Behavior!
fflush()should only be used on streams open for output, not input.