Print the data of a vector in a struct

4

I want to make a birthday date registration system. The final doubt is: how do I print the anniversary: 1/12/1990, 12/2/1977, 09/13/1999 and 19/04/1987. They would be the examples of the printing of the dates, but I get several numbers that have nothing to do. Follow the code.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int dia;
    int mes;
    int ano;
} data;

int main()
{
    data data_aniversario[4];
    int i=0;

    while (i<4)
    {
        printf("Preencha sua data de nascimento\n\n");
        printf("Digite o dia: ");
        scanf("%d", &data_aniversario[i].dia);
        printf("\n\nDigite o mes: ");
        scanf("%d", &data_aniversario[i].mes);
        printf("\n\nDigite o ano: ");
        scanf("%d", &data_aniversario[i].ano);
    }

    printf("\n\nA data do seu aniversário é: \n");
    printf("%d/%d/%d\n", data_aniversario.dia, data_aniversario.mes, data_aniversario.ano);
    system ("pause");
    return 0;
}
    
asked by anonymous 14.11.2014 / 14:22

3 answers

4

There are two problems in your code:

  • You need to increment the variable that controls the loop where it asks for the data. You're saving all four dates in the same place and I do not think it's what you want.
  • When you print the birth dates you are not using a loop . You are trying to print the array and not its elements. It is not possible to print the array as a whole, the array is just a memory address, hence strange numbers appear. In the specific case you can not even print the elements but the members of these elements, but this was right in your code, you just needed to index the array the same way you did when you were reading the data.

In addition, the variable name, although not causing problems in the code, passes the wrong idea of what it holds.

See:

#include <stdio.h>

typedef struct {
    int dia;
    int mes;
    int ano;
} data;

int main() {
    data data_aniversario[4];
    for (int i = 0; i < 4; i++) {
        printf("Preencha sua data de nascimento\n\n");
        printf("Digite o dia: ");
        scanf("%d", &data_aniversario[i].dia);
        printf("\n\nDigite o mes: ");
        scanf("%d", &data_aniversario[i].mes);
        printf("\n\nDigite o ano: ");
        scanf("%d", &data_aniversario[i].ano);
    }
    for (int i = 0;i < 4;i++) {
        printf("\n\nA data do seu aniversário é: \n");
        printf("%d/%d/%d\n", data_aniversario[i].dia, data_aniversario[i].mes, data_aniversario[i].ano);
    }
}

See running on ideone . And at Coding Ground . Also put it in GitHub for future reference .

    
14.11.2014 / 14:43
2

When you print the dates, you are doing:

printf("%d/%d/%d\n", data_aniversario.dia, data_aniversario.mes, data_aniversario.ano);

And it should be data_aniversario[0].dia , ranging from 0 to 3 to print all dates.

Note: an i ++ is also missing to stop while . Or you can use for .

while (i<4)
{
     // seu código
     i++;
}
    
14.11.2014 / 14:37
1

data_aniversario is an array with 4 elements data . You do the input of 4 dates and save in this array. To print you will have to use a loop that traverses all elements of data_aniversario .

Anything of the sort:

for(i = 0; i < 4;i++)
{
    printf("\n\nA data do seu aniversário é: \n");
    printf("%d/%d/%d\n", data_aniversario[i].dia, data_aniversario[i].mes, data_aniversario[i].ano);
} 
    
14.11.2014 / 14:38