-1

I'm creating a program where I ask the user to enter some of his personal informations, but for some reason I can't figure it out. When I ask the user to enter his birth date and his email, the message appears in a weird way for the user.

#include <stdio.h>
#define MAX  50
char name[MAX],date[MAX],email[MAX],cpf[MAX],tel[MAX],contatip[MAX],esc3[MAX],esc4[MAX];

int main()
{
    printf("\nEnter your name: ");
        scanf("%s",name);
    printf("\nBirth date:"); // The problem is here
        gets(date);
    printf("\Email: ");
        gets(email);
    printf("\nEnter your CPF: ");
        gets(cpf);
    printf("\nCelphone: ");
        gets(tel);
}
tripleee
  • 175,061
  • 34
  • 275
  • 318
gpmotta21
  • 1
  • 2
  • 6
    "appears in a weird way". What does that mean? Please show the exact input, expected result and actual result. – kaylum Jun 16 '21 at 02:08
  • 9
    OT: [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). Use `fgets` instead. – kaylum Jun 16 '21 at 02:09
  • 2
    `scanf("%s",name);` reads until whitespace is encountered so if you enter a first and last name then the last name will go into the birth date. If you even enter a first name and then press space and then enter the scanf will stop after the first name and leave the enter key in the buffer and the birth date will be empty. You need to use fgets() for all the entries. – Jerry Jeremiah Jun 16 '21 at 02:11
  • you might want to add `fflush()` after every `printf` before `fgets`. – Serge Jun 16 '21 at 02:14
  • Here is an example of scanf leaving stuff in the buffer: https://onlinegdb.com/5DyozWDn7 – Jerry Jeremiah Jun 16 '21 at 02:16
  • 2
    Don't use `gets`. It is no longer supported, and hopefully won't even exist at some point. The man page should explain this. Use `fgets` instead. And change the `scanf` to `fgets` as well. – Tom Karzes Jun 16 '21 at 02:24
  • 1
    @Serge you do not need `fflush()`, when an input function such as `fgets()` or `scanf()` is invoked, output streams are flushed. – David C. Rankin Jun 16 '21 at 02:24
  • You can refer to [this](https://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/) article. As others said, use `fgets()`. – Shubham Jun 16 '21 at 02:32

1 Answers1

0
#include <stdio.h>

#define MAX 50

char name[MAX], date[MAX], email[MAX], cpf[MAX], tel[MAX], contatip[MAX], esc3[MAX], esc4[MAX];

int main()
{
    printf("\nEnter your name: ");
        fgets(name, MAX, stdin);
    printf("\nBirth date: ");
        fgets(date, MAX, stdin);
    printf("\nEmail: ");
        fgets(email, MAX, stdin);
    printf("\nEnter your CPF: ");
        fgets(cpf, MAX, stdin);
    printf("\nCellphone: ");
        fgets(tel, MAX, stdin);
    // printf("\n%s", name);
    // printf("%s", date);
    // printf("%s", email);
    // printf("%s", cpf);
    // printf("%s", tel);
}
Edmund
  • 332
  • 1
  • 9