0

I dont really know where im wrong. it gives me "use of undeclared identifier 'num'" i allready tried to solve it but i have no clue of what is going on

#include <stdio.h>

int maxc = 0, total = 0, posc, selec =0, poscGanador = -1;
char cand[] = {'A', 'B', 'C', 'D'};
int candidato[num][4];
int totalVotos[num];
double porc[4]; // vector para calcular porcentajhe


int ingresoDatos () {
    int num;
     // ingresar datos
    do{
        printf("Ingrese su distrito : "); scanf("%i",&num);
    } while (num<=0);

    for(int i = 0; i<num; i++) {
        printf("Distrito N. %i\n\n", (i+1));

        for (int j = 0; j<4; j++) {
            printf("Ingrese los votos del candidato %c %s \n\n", cand[j], " : ");
            scanf("%i",&candidato[i][j]);
            totalVotos[j] = 0;//inicializo este arreglo aqui pq luego hay bugs xd
        }
        printf("\n");
        
    }

    // contar votos totales
    int cont = 0;
    for (int i =0; i<num;i++) {
        cont = 0;
        for (int j = 0; j<4; j++) {
            totalVotos[cont] = totalVotos[cont] + candidato[i][j];
            // contador debe incrementar
            cont++;
        }
    }
    return num;
}

void menu()
{
    printf("\n\n");
    printf("Seleccione el numero de la opcion que desea realizar seguido de ENTER. \n");
    printf("1.Votos por distrito\n");
    printf("2.Numero de Votos totales.\n"); 
    printf("3.Resultados de mayor a menor.\n");
    printf("4.Salir.\n");
    scanf("%i",&selec);
}


void tabla(num) {
     // mostrar los fucking datos
    printf("Distrito\tCandidato\tCandidato\tCandidato\tCandidato\n");
    printf("\t\tA\t\tB\t\tC\t\tD\n");
    for (int i = 0;i<num;i++) {
        printf("\n%i %s", (i+1), "\t\t");
        for (int j=0;j<4;j++) {
            printf("%i%s", candidato[i][j], "\t\t");
        }
    }

    printf("\n");
    printf("\nVotos\t\t");
    for (int i =0;i<4;i++){
        printf("%i %s", totalVotos[i], "\t\t");
        total+=totalVotos[i]; // acumula todos los votos
    }

    for (int i = 0;i<4;i++) {
        if (totalVotos[i]>maxc){
            maxc = totalVotos[i];
            posc = i; // encontrar la posicion de candidato mas votado
        }
    }
    
    printf("\n\n\nEl Candidato %c %s %i %s \n\n", cand[posc], "fue el mas votado con ", maxc, " votos");
}

void ganador(){
    // declarar ganador
    for (int i = 0;i<4 && poscGanador == -1 ;i++) {
        if (porc[i]>50) {
            poscGanador = i;
            
        }
    }
    
    if (poscGanador == -1) {
        // si es -1 es pq no hay ganador aun
        printf("\nAun no hay un candidato Ganador de las eslecciones");
    }else {
        printf("\nEl Ganador de las elecciones es : El Candidato %c\n\n", cand[poscGanador]);
    }

}
    
void porcentajes() {
     // mostrar porcentajes
    for (int i = 0; i<4;i++) {
        porc[i] = (totalVotos[i]*100/total);
        printf("Candidato %c %s %f %s", cand[i], " : ", porc[i], "%\n");
    }
    printf("\n");
    
}
   
int main() {
    ingresoDatos();
    do
    {
        menu();
        switch (selec)
        {
            case 1: 
                tabla(num);
            break;
            
            case 2:
                ganador();
            break;
            
            case 3:
                porcentajes();
            break;
        }
    }
    while(selec != 4);
    
    if(selec == 4)
    {
        printf("\n");
        printf("¡Adios!\n");
    }
    
}

enter image description here

0

It's a scope issue. You're getting compile error because this code is trying to use an undeclared variable, num, to declare the size of a global variable array. num doesn't exist at all at this point:

int candidato[num][4];
int totalVotos[num];

It's a problem later too, because if you do successfully declare the size of the arrays, that size is fixed. There's no way to declare these arrays at the correct size because there's no way to predict how large they will be. All you can do is make a guess of what the maximum size would be, add some extra, just in case, and declare a fixed size. That's going to be either too small, or a waste of memory when it's too big, as well as an invitation to bugs.

Later, num only exists in the function(s) where it is declared. Also, when declared in different functions, each function contains a different version of variables with the same name and they're all unrelated. When I say 'functions', that also includes main().

So, the problem will reappear in main(). The var num is not declared in main, so it doesn't exist when the code tries to use it in the switch statement. It hasn't been declared and no value has been assigned to it.

Remember too, values in variables are passed by copy from main to the functions, or from function to function. That means that the variable in the calling function will remain unchanged, while the called function may or may not change the value in it's local version of the var.

The only way this isn't true is if the var is a global variable. But, well, see below.

Programming tip: This program has numerous global variables. It's a bad practice to use global variables unless absolutely necessary. With so many, I seriously doubt that this many (or perhaps any of them) are necessary. Global variables are an invitation to software bugs, especially when multiple functions are involved. You should try to eliminate using them as globals and set them up as locals.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .