Struct ! Error on line 47 at the moment it compiles, Follow the program in c

Asked

Viewed 52 times

0

#define MAX 50
struct{
    int ra;
    char nome[MAX];
    float prova;
}aluno[5];

int main(){
    struct aluno;
    int i;
    int j;

    printf("Determine o Nome do Aluno %d: ", i+1);
    printf("Determine a Matricula do Aluno &d: ", i+1);
    for(i=0;i<5;i++){
        fgets(aluno[i].nome,MAX,stdin);
        scanf("%i", &aluno[i].ra);
    for(j=0;j<3;j++){
        printf("Determine a nota da %d Prova: ", j+1);
        scanf("%f", &aluno[i].prova[j]);
      }
    }
return 0;
}
  • 6

    Your code has 24 lines. How is it wrong in 47?

1 answer

3

When you do that:

    struct aluno;

You are trying to declare a variable incorrectly. Remove this line as aluno was previously declared as a 5 position array with the type of the struct.

Notice that line:

    printf("Determine a Matricula do Aluno &d: ", i+1);

Instead of &d, you should use %d.

Those first two printfshould be within the for, and not before him.

In his second for, you use three notes, but you didn’t declare the three in struct.

I think what you wanted was this:

#define MAX 50
struct {
    int ra;
    char nome[MAX];
    float prova[3];
} aluno[5];

int main() {
    int i;
    int j;

    for (i = 0; i < 5; i++) {
        printf("Determine o Nome do Aluno %d: ", i + 1);
        fgets(aluno[i].nome, MAX, stdin);

        printf("Determine a Matricula do Aluno &d: ", i + 1);
        scanf("%i", &aluno[i].ra);

        for (j = 0; j < 3; j++) {
            printf("Determine a nota da %d Prova: ", j + 1);
            scanf("%f", &aluno[i].prova[j]);
        }
    }
    return 0;
}
  • I didn’t understand this student[5] below the struct?

  • @YODA To say that student is an array of 5 positions and that each position has an element with that type struct there.

  • Don’t need typedef? In case the struct has 3 variables and the other two will be filled as?

  • @YODA Would need the typedef if it were to declare variables of the same type struct in other places, which is almost always a good idea. But in this specific case, this is not yet necessary.

Browser other questions tagged

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