Print vector data in a struct

Asked

Viewed 729 times

4

I want to do a birth-date registration system. The final question is: as I print the birthday: 1/12/1990, 12/2/1977, 13/09/1999 and 19/04/1987. Would be the examples of the printing of dates, but I appear several numbers that has 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;
}

3 answers

4


There are two problems in your code:

  • You need to increment the variable that controls the loop where ask for the data. You are storing the four dates in the same location and think it is not what you want.
  • When you print the birth dates you are not using one loop. Are you trying to print the array and not its elements. Unable to print array as a whole, the array It’s just a memory address, then weird numbers come up. In the specific case you can not print the elements but the members of these elements, but this was right in your code, just needed to index the array the same way you did correctly when reading the data.

In addition the variable name, although it does not cause problems in the code, gives wrong idea of what it keeps.

Behold:

#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);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

When you print out the dates, you’re doing:

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

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


Note: Also missing an i++ to stop the while. Or you can use a for.

while (i<4)
{
     // seu código
     i++;
}

1

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

Something like that:

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);
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.