C code does not show full list

Asked

Viewed 38 times

0

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

int main()
{

    struct aluno{

        char nome[30];
        int n_matricula;
        char curso[30];
    };

    struct aluno alunos[5];
    int numero;

    for(numero=1; numero<=5; numero++)
    {
        printf("Nome do aluno %i.................: ",numero);   gets(alunos[numero].nome);
        printf("Curso do aluno %i................: ",numero);   gets(alunos[numero].curso);
        printf("Numero da matricula do aluno %i..: ",numero);   scanf("%i",&alunos[numero].n_matricula);
        printf("\n\n\n");
    }
    printf("\n\n_____________________________________________\n\n");
    printf("Alunos:\n\n");

    for(numero=1; numero<=5; numero++)
    {
        printf("\n----------------------\n");   printf("%s",alunos[numero].nome);   printf("\n----------------------\n");
        printf("\n----------------------\n");   printf("%s",alunos[numero].curso);  printf("\n----------------------\n");
        printf("\n----------------------\n");   printf("%s",alunos[numero].n_matricula);    printf("\n----------------------\n");
    }
    return 0;
}
  • 1

    Do not elaborate questions only with code. Detail your problem as much as possible so that those who read your question can understand. I can already say that there are some mistakes how to use %s to read numbers, and mix gets with scanf

  • Thank you for warning

  • The index of an array in C part of 0 and not 1.

  • First of all, look how and why to accept an answer.

1 answer

0


C iterations start at 0, so the for stays:

for(numero = 0; numero < 5; numero++)

In: printf("%s",alunos[numero].n_matricula); is printf("%d",alunos[numero].n_matricula);

To mix scanf and gets you must have to unload the entrance, see: https://www.inf.pucrs.br/~pinho/Laproi/Fflush/Corrigescanfgets.html

Moreover gets is deprecated and shall not be used.

Below how you can do using fgets in everything, with atoi for n_matricula:

int main()
{
    struct aluno{

        char nome[30];
        int n_matricula;
        char curso[30];
    };

    struct aluno alunos[5];
    char matricula[10];
    int numero;

    for(numero=0; numero<5; numero++)
    {
        printf("Nome do aluno %i.................: ",numero);   fgets(alunos[numero].nome, 30, stdin);
        printf("Curso do aluno %i................: ",numero);   fgets(alunos[numero].curso, 30, stdin);
        printf("Numero da matricula do aluno %i..: ",numero);   fgets(matricula, 10, stdin);
        alunos[numero].n_matricula = atoi(matricula);
        printf("\n\n\n");
    }
    printf("\n\n_____________________________________________\n\n");
    printf("Alunos:\n\n");

    for(numero=0; numero<5; numero++)
    {
        printf("\n----------------------\n");   printf("%s",alunos[numero].nome);   printf("----------------------\n");
        printf("\n----------------------\n");   printf("%s",alunos[numero].curso);  printf("----------------------\n");
        printf("\n----------------------\n");   printf("%d",alunos[numero].n_matricula);    printf("\n----------------------\n");
    }
    return 0;
}

Browser other questions tagged

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