Why don’t you get in the loop?

Asked

Viewed 89 times

0

I’m storing student names and grades and trying to display name, grades and media. The problem is that the program is skipping the loop of notes and is only taking 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;
}
  • in the for it is necessary to put cont2=0, just do a mini debug to discover this flaw

  • True, Fábio. Only garbage is appearing.

  • I found the error. It’s in formatting the data.

  • I thought I’d found out, but I was wrong. Still giving trouble.

1 answer

2


The problem is using 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 do not assign the value 0 to the variable after the first loop, it will start the second loop with the end value of the first loop, ie MAX_NOTAS - 1.

To solve this problem, you can use different variables in each loop or initialize the variable cont2 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);
  • Thanks, Marcelo. I got it. I had other mistakes too , but have been corrected.

  • You are welcome! I also noticed that the average calculation was not working, I don’t know if you have corrected this error as well.

  • Simpler and more intuitive would be to make the assignment to 0 within the for which is basically the purpose of the first block of the for

Browser other questions tagged

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