Why do not you enter the notes loop?

0

I am storing student names and notes and trying to display the name, grade, and grade. The problem is that the program is jumping the notes loop and is just picking up the name.

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

/*
Síntese
Objetivo: Calcular a média das notas de um aluno
Entrada: Nome, Nota 1, nota 2, nota 3, e nota 4
Saída: Nome, notas do aluno e média
*/


#define MAX_NOME 100
#define MAX_NOTAS 4
#define VETOR_MAX 50
typedef struct Alunos{
    char nome[MAX_NOME];
    int notas[MAX_NOTAS];
}Aluno;

int leValidaOpcao();
int main(int argc, char *argv[]) {
    int cont=0, cont2=0, qtdAlunos=0, soma;
    char continuar = ' ';
    float media=0.0;

    Aluno * aluno1 = malloc(qtdAlunos * sizeof(Aluno));


    do{
        printf("Informe o %d%c nome :", cont+1, 167);
        scanf(" %[^\n]s", (aluno1+cont)->nome);

        for(cont2; cont2 < MAX_NOTAS;cont2++){
            printf("Informe a nota %d:", cont2+1);
            scanf("%d", &(aluno1+cont)->notas[cont2]);
            soma += (aluno1+cont)-> notas[cont2];
            system("cls");
        }

        printf("Nome = %s\n", (aluno1+cont)->nome);
        for(cont2; cont2 < MAX_NOTAS; cont2++){
            printf("Nota[%d] =  %d\n", cont2+1, (aluno1+cont)->notas[cont2]);
        }
        printf("\nMedia = %f", soma/MAX_NOTAS);

        continuar = leValidaOpcao();
        cont++;
    }while(continuar == 's' && cont < VETOR_MAX);


    free(aluno1);
    return 0;
}
int leValidaOpcao(){
    char opcao = ' ';
    int flag = 1;


    do{
        printf("Deseja continuar (S - sim ou Nao - N)");
        scanf(" %c", &opcao);
        opcao = tolower(opcao);

        if(opcao != 's' && opcao != 'n'){
            printf("\nOpcao invalida!\n");
            flag = 0;
        }
    }while(!flag);
    return opcao;
}
    
asked by anonymous 19.09.2018 / 21:20

1 answer

2

The problem is to use the same variable cont2 in both loops.

    for(cont2; cont2 < MAX_NOTAS;cont2++){
        printf("Informe a nota %d:", cont2+1);
        scanf("%d", &(aluno1+cont)->notas[cont2]);
        soma += (aluno1+cont)-> notas[cont2];
        system("cls");
    }

    printf("Nome = %s\n", (aluno1+cont)->nome);

    for(cont2; cont2 < MAX_NOTAS; cont2++){
        printf("Nota[%d] =  %d\n", cont2+1, (aluno1+cont)->notas[cont2]);
    }
    printf("\nMedia = %f", soma/MAX_NOTAS);

Because you initialize does not assign the value 0 to the variable after the first loop, it will start the second loop with the final value of the first loop, that is, MAX_NOTES - 1.

To solve this problem, you can use different variables in each loop or initialize the cont2 variable again, for example:

for(cont2; cont2 < MAX_NOTAS;cont2++){
    printf("Informe a nota %d:", cont2+1);
    scanf("%d", &(aluno1+cont)->notas[cont2]);
    soma += (aluno1+cont)-> notas[cont2];
    system("cls");
}

printf("Nome = %s\n", (aluno1+cont)->nome);

cont2 = 0;

for(cont2; cont2 < MAX_NOTAS; cont2++){
    printf("Nota[%d] =  %d\n", cont2+1, (aluno1+cont)->notas[cont2]);
}
printf("\nMedia = %f", soma/MAX_NOTAS);
    
19.09.2018 / 22:04