c int comparrison for finish loop -


i have following function do while loop inside:

void getfivenumericvalues() {     int validationresult;     char input[5];         {         printf("please enter 5 digits:\n");         validationresult = scanf("%s", &input);          printf("validation result: %d\n", validationresult);     } while (validationresult != 1);     // while (!(validationresult == 1));     // while (validationresult > 1 || validationresult < 1); } 

the loop doesn't finish when validationresult == 1.
my image missing here?

input[] small hold 5 character string, since needs hold terminating '\0' in addition 5 input characters. if entering 5 characters have buffer overflow , undefined behaviour. change @ least:

char input[6]; 

furthermore line:

validationresult = scanf("%s", &input); 

should be:

validationresult = scanf("%s", input); 

since input pointer.

or better yet:

validationresult = scanf("%5s", input); 

which prevent buffer overflow, if have invalid input.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -