C Language Database

Asked

Viewed 214 times

-1

I’m trying to solve this question:

Banco de Dados

But I’m not getting the proper compilation done. My code is this:

#include <stdio.h>

typedef struct{
int idade;
char nome[50];
char sexo[2];
char estado_civil[2];
int qtd_amigos;
int qtd_fotos;
} cliente;

int main() {

int i, qtd;

scanf("%i", &qtd);

cliente clientes[qtd];  

for (i = 0; i < qtd; i++) {

    scanf(&clientes[i].idade);
    scanf(&clientes[i].nome,50, stdin);
    scanf(&clientes[i].sexo,2, stdin);
    scanf(&clientes[i].estado_civil, 2, stdin);
    scanf(&clientes[i].qtd_amigos);
    scanf(&clientes[i].qtd_fotos);
}

for (i = 0; i < qtd; i++) {
    printf("Idade: %d\n", clientes[i].idade);
    printf("Nome: %s\n", clientes[i].nome);
    printf("Sexo: %s\n", clientes[i].sexo);
    printf("Estado Civil: %s\n", clientes[i].estado_civil);
    printf("Numero de amigos: %d\n", clientes[i].qtd_amigos);
    printf("Numero de fotos: %d\n", clientes[i].qtd_fotos);
}
    printf("\n");

return 0;
}
  • There is no reason why you should declare the variables gender and marital status as char[2], declare them only as char (after all they will occupy a single character and there is no need to be an array). Your scanf are all wrong, you made a mix of the available functions in <stdio. h>.

1 answer

0


Your scanf were all wrong.

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

    typedef struct{
    int idade;
    char nome[50];
    char sexo[2];
    char estado_civil[50];
    int qtd_amigos;
    int qtd_fotos;
    } cliente;

    int main() {

    int i, qtd;
    scanf("%d", &qtd);

    cliente clientes[qtd];  

    for (i = 0; i < qtd; i++) {
        scanf("%d",&clientes[i].idade);
        scanf("%s",&clientes[i].nome);
        scanf("%s",&clientes[i].sexo);
        scanf("%s",&clientes[i].estado_civil);
        scanf("%d",&clientes[i].qtd_amigos);
        scanf("%d",&clientes[i].qtd_fotos);
    }

    for (i = 0; i < qtd; i++) {
        printf("Idade: %d\n", clientes[i].idade);
        printf("Nome: %s\n", clientes[i].nome);
        printf("Sexo: %s\n", clientes[i].sexo);
        printf("Estado Civil: %s\n", clientes[i].estado_civil);
        printf("Numero de amigos: %d\n", clientes[i].qtd_amigos);
        printf("Numero de fotos: %d\n", clientes[i].qtd_fotos);
    }
        printf("\n");

    return 0;
    }
  • Hello, just now I had made these corrections. Anyway, thank you!

  • Note that in the reading of strings in the scanf (%s) you do not enter the address of the variable ( this & initial) because you are already providing the address of the initial position of the string. Or report: scanf("%s", clients[i]. sex); or: scanf("%s",&clients[i]. sex[0]);.

  • Yes, you’re right, my mistake. Before I had %c, there was the "&", then I corrected to %s and forgot to remove &. My slip

Browser other questions tagged

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