Know how many positions were filled in a vector in C

0

I can not display the filled positions in C. I need to get back which positions are missing to be filled and those that are filled.

ex: "So far there are 3 registered students, thus allowing the insertion of a maximum of 7 more students."

#include <stdio.h>
#include <time.h>
#include <math.h> 
#include <locale.h>

int main(void){
    set locale(LC_ALL,"portuguese");
    printf("###################### ENTRE COM OS DADOS ABAIXO ##########################\n\n");
    //chamada da function
    imprimemenu();

    return(0);
    system("pause");
}
    struct Alunos{
        char name[10];
        char sobrname[10];
        int matri;

        //variavel data de nascimento
        unsigned int dia,mes,ano;

        //data atual que o ussuario digitar
        unsigned int atualDay,atualMonth,atualYer;

        unsigned int resultDia,resultAnos;
    };

//funcoes do algoritmo;
imprimemenu(void){
    unsigned int esc;
    do{
        printf("Menu de Opções:\n");

        printf("1-Cadastrar Aluno:\n");
        printf("2-Calcular Idade:\n");
        printf("3-Imprimir Aluno:\n");
        printf("4-Pesquisar Aluno:\n");
        printf("5-Excluir Aluno:\n");
        printf("6-Esvaziar lista de Alunos:\n");
        printf("7-Quantidade de Alunos cadastrados:\n");
        printf("0-sair:\n");
        scanf("%i",&esc);
            switch(esc){
            case 1:
                system("cls || clear");
                printf("DIGITE ABAIXO OS DADOS:\n");
                inseriraluno();
                break;
            case 2:
                system("cls || clear");
                //agestudents();
                break;
            case 3:
                system("cls || clear");
                listalunos();
                break;  
            case 4:
                system("cls || clear");
                //searchstudents();
                break;
            case 5:
                system("cls || clear");
                //excluirstudents();
                break;
            case 6:
                system("cls || clear");
            case 7:
                system("cls || clear");
                contstudents();
                break;
            case 0:
                system("cls || clear");
                printf("PROGRAMA FINALIZADO PELO USUÁRIO!\n\n");
                break;
            default:
                system("cls || clear");
                printf("NENHUMA OPÇÃO SELECIONADA!\n\n");
                printf("POR FAVOR SELECIONE UMA DAS OPÇÕES ABAIXO\n\n");
                imprimemenu();  
            }
    }while(esc);
}
inseriraluno(){
    int conta = 0;  
    //definindo o nome da minha estrutura
    struct Alunos alunos[10];

    //data do dia do cadastro do usuario;
        printf("\nDIGITE A DATA DE HOJE:");
        printf("\nDigite o Dia:");
        scanf("%d",&alunos[conta].atualDay);

        printf("\nDigite o Mes:");
        scanf("%d",&alunos[conta].atualMonth);

        fflush(stdin);
        printf("\nDigite o Ano:");
        scanf("%d",&alunos[conta].atualYer); 

    for(conta = 0; conta<1; conta++){
        fflush(stdin);

        //dados do usuario
        printf("\nDigite o nome do %d Aluno:",conta+1);
        gets(alunos[conta].name);

        printf("\nDigite o sobrenome do Aluno:");
        scanf("%s",&alunos[conta].sobrname);    

        printf("\nDigite o numero de matrícula do Aluno:");
        scanf("%d",&alunos[conta].matri);


        //data de nascimento do usuario
        printf("\nDIGITE A SUA DATA DE NASCIMENTO:");
        printf("\nDigite o Dia:");
        scanf("%d",&alunos[conta].dia);

        printf("\nDigite o Mes:");
        scanf("%d",&alunos[conta].mes);

        fflush(stdin);
        printf("\nDigite o Ano:");
        scanf("%d",&alunos[conta].ano);

    }
}

contstudents(struct Alunos alunos[10]){
    unsigned int contador;
    unsigned int total = 0;
    unsigned int resto = 0;
    for(contador = 0; contador<alunos; contador++){
        printf("O tamanho é: %d\n\n",sizeof(alunos[contador]));

        return 0;
    }
    //printf("\n\nAté o momento existem %s alunos cadastrados, possibilitando assim a inserção de, no máximo, mais "" alunos.",total;   


}
    
asked by anonymous 09.11.2016 / 19:43

1 answer

1

This code has many errors (some are not preventing the code from working, but it is not the right way to do it), I will not try to fix them because I would practically have to rewrite everything. To solve this question there are basically two outputs:

  • Maintain a counting variable and increment or decrement as you enter or remove students. You can even create a structure to hold that variable next to the array , it would be the most professional way.

  • Place a null character at the beginning (it can be anywhere) of each element of the array when it initializes and overlaps when it inserts, and when it removes, puts the null character back there. So you can walk element by element and it counts as you did in the contstudents() function using a if to check if you got a null, when you find a null, stop counting because you do not have any elements. li>

    Note that removing is not as simple an operation as it may seem, you would probably have to rearrange the entire array .

        
  • 09.11.2016 / 20:01