Know how many positions were filled in a vector in C

Asked

Viewed 769 times

0

I cannot display the filled positions in C. I need to return which positions are missing to be filled and which are filled.

ex:"So far there are 3 registered students, thus enabling 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;   


}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

This code has many errors (some do not stop 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 issue there are basically two exits:

  1. Maintain a counter variable and increase or decrease as you insert or remove students. You can even create a structure to keep this variable next to the array, would be the most professional way.

  2. Place a null character at the beginning (can be anywhere) of each element of the array when initializes it and overwrites it when you enter it, and when you remove it, put the null character back there. So you can walk element by element and count as you did in the function contstudents() (bad name) using a if to check if you have reached where you have a null, when you find a null, stop counting because you have no more elements.

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

  • Thanks bigown, I’m modifying the code as you suggested me. As I’m beginner in C, I’m picking to make this function work. You could send an example ?

  • As I said to make an example I would have to rewrite the code, I’m not in the mood to do that. Nor is the purpose of the site. If apergunta were more specific.

  • Got it here. Thanks for your help.

Browser other questions tagged

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