Program doesn’t execute gets() after scanf(), even using fflush(stdin)

If flushing std doesn’t work, then try reading in the extra characters and discarding, as suggested here. This will work: #include <string.h> #include <stdio.h> int main(){ char nombre[10]; char mensaje[80]; int c; printf(“Type your name:\n”); scanf(“%9s”, nombre); while((c= getchar()) != ‘\n’ && c != EOF) /* discard */ ; printf(“Now, type a message:\n”); gets(mensaje); printf(“%s:%s”,nombre,mensaje); … Read more

Disable warning: the `gets’ function is dangerous in GCC through header files?

The obvious answer is to learn from what the compiler is trying to tell you – you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns. #define BUFFER_SIZE 100 char buff[BUFFER_SIZE]; gets( buff); // unsafe! fgets( buff, sizeof(buff), stdin ); // safe

Input in C. Scanf before gets. Problem

Try: scanf(“%d\n”, &a); gets only reads the ‘\n’ that scanf leaves in. Also, you should use fgets not gets: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ to avoid possible buffer overflows. Edit: if the above doesn’t work, try: … scanf(“%d”, &a); getc(stdin); …

C – scanf() vs gets() vs fgets()

Never use gets. It offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory). Avoid using scanf. If not used carefully, it can have the … Read more

tech