-1

My code has a bug in command gets() cant input string name. How can I do this?

#include <stdio.h>
#include <string.h>

struct letter {
    char name[20];
    char address[30];
    char message[40];
};

int n,i;

main() {
    printf("Please enter number of employee: ");
    scanf("%d",&n);
    struct letter first[n];
    //1. Keep an information
    for(i=0; i<n; i++) {
        //gets() does not work what wrong with this
        printf("Enter name[%d] : ",i);
        gets(first[i].name);

        printf("\nEnter address[%d] : ",i);
        scanf("%s",first[i].address);
        strcpy(first[i].message, "How r u?");
    }

    // Show an information
    for(i=0; i<n; i++) {
        printf("\nNAME[%d] is %s",i,first[i].name);
        printf("\nAddress[%d] is %s",i,first[i].address);
        printf("\nMessage : %s",first[i].message);
    }
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
Max
  • 1
  • 1
    To begin with, never *ever* use `gets`.It's a [dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that has been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Nov 20 '18 at 07:34
  • That's right, `gets` will not work because no such function exists in the C language. It has been removed. See [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). You need a new source of learning C, your current one is horribly outdated. – Lundin Nov 20 '18 at 07:34
  • There are also a couple of other problems. For example don't use global variables, especially if those are only used inside a single function. And the [`main`](https://en.cppreference.com/w/c/language/main_function) function is required to be declared as returning an `int` (and take a `void` as argument if don't want command-line arguments). You also don't add bounds for the `scanf` calls so they can write out of bounds of your arrays. – Some programmer dude Nov 20 '18 at 07:39
  • i'm sorry i'm really dont know with this i know what a problem but i cant explain i'm studying in grade 5 from school in thailand i'm so sorry i just want to know about code and c programing – Max Nov 20 '18 at 07:51
  • You don't need to *explain*, you only have to tell us what happens on your system when you run this. – Jongware Nov 20 '18 at 09:37
  • DANGER! DANGER! DANGER! They allways start to bowl this when they see gets(). I would say you shouldn't use scanf.(). LOL. With gets you just can get buffer overflow. But it's ok to use gets() in small programs for learning purposes. However it is better to replays it with fgets(). – purec Nov 20 '18 at 11:04
  • main() should return int. – purec Nov 20 '18 at 11:05

2 Answers2

1

If you check the man of gets function, we can see that is depreceated :

gets - get a string from standard input (DEPRECATED)

An alternative is the POSIX function getline() which might be available on your system:

#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);

The buffer is allocated or reallocated with malloc() and its size is updated into *n. The initial values should be lineptr = NULL and n = 0.

Another alternative : fgets() --> Man fgets

You have a lot of example on Stack and google how to do it. Good luck.

Simon Provost
  • 356
  • 1
  • 2
  • 15
0

Just to clarify, why it does not work. According man pages:

gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF ...

When you do scanf("%d",&n);, newline symbol is still in stdin and gets reads empty string. So, if you will add getchar after the first scanf and the scanf in the loop, all should work fine, but again, it's better to use gets alternatives.