I've seen various posts of how using scanf before fgets might cause problems. However, it makes no sense to me why adding it AFTER should cause the first statement to break.
This is what I have:
unsigned char key;
unsigned char message[100];
printf(" Please input a message \n ");
fgets(message, MAX_BUF, stdin);
printf("Your message is: %s \n", message);
// Read user input for message
printf(" Please input a numeric key\n ");
scanf("%d", &key);
printf("Your message is: %s \n", message);
printf("strlen(message) = %d \n", strlen(message));
And this is a "trial run":
Please input a message
hello
Your message is: hello
Please input a numeric key
123
Your message is:
strlen(message) = 0
Like this, the message variable ends up empty no matter the input. However, if I comment out the line with scanf, everything works (except I can't assign key).
Help!
fgets()
andscanf()
to help understand the issues. Showing your input helps.