Error in structure fix using functions

0

I got the following error

  

44 33 C: \ Users \ Miguel \ Desktop \ ss.cpp [Error] could not convert '(STUDENT *) (& NEW)' from 'STUDENT *' to 'STUDENT'

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

struct ALUMNO{
    char nombre[30];
    char edad[2];
    char sexo[10];
    char calle[15];
    char numero[10];
    char colonia[15];
    char telefono[10];  
    int carrera;
    int id;
}NUEVO;

int contInd=0;
int contAdm=0;
int contPsi=0;
int ContGen=0;

void agregarAlumno(ALUMNO);
void imprimeAlumnos(ALUMNO);
void impNumeros();

int main(){
    struct ALUMNO NUEVO[9];

    int op;
    do{
        //system("cls");
        printf("***MENU DE ESCUELA***\n\n");
        printf("1.- Agregar alumno.\n");
        printf("2.- Imprimir lista de alumnos.\n");
        printf("3.- Imprimir cifras de las carreras.\n");
        printf("4.- Salir.\n\n");
        printf("Seleccione su opcion: ");
        scanf("%d", &op);

        //system("cls");

        switch(op) {
            case 1:
                agregarAlumno(NUEVO);
            break;

            case 2:
                imprimeAlumnos(NUEVO);
            break;

            case 3:
                impNumeros();
            break;

            case 4:
                return 0;
            break;

            default:
                printf("Opcion invalida! ");
            break;
        }
    }while(op!=4);

        //system("Pause");

}


void agregarAlumno(struct ALUMNO NUEVO[]) {
    int indexCar;

    for (int x=0;x<=9;x++){
    printf("***AGREGAR NUEVO USUARIO***\n\n");
    printf("\n0 = Ingenieria Industrial \n1 = Administracion \n2 = Psicologia\n ");
    printf("\n\nIngresa el ID de la carrera al que ingresara: ");
    scanf("%d", &indexCar);
    NUEVO[x].carrera=indexCar;

    while(indexCar < 0 || indexCar-1 >= 2){
       printf("\nNo existe ninguna carrera con ese id!");
       printf("\n0 = Ingenieria Industrial \n1 = Administracion \n2 = Psicologia\n ");
       printf("\n\nIngresa el ID de la carrera al que ingresara: ");
       scanf("%d", &indexCar);
    }

    fflush(stdin);

    if  (NUEVO[x].carrera==0) 
     {contInd++;
      ContGen++;
     }
    if  (NUEVO[x].carrera==1) 
     {contAdm++;
      ContGen++;
     }

    if  (NUEVO[x].carrera==2) 
     {contPsi++;
      ContGen++;
     }

        fflush(stdin);

        printf("\nIngresa su nombre: ");
        //gets(NUEVO[c].nombre);
        //scanf( "%s", &NUEVO[c].nombre);
        scanf("%[^\n]s", &NUEVO[x].nombre);

    /*  printf("\nIngresa su edad: ");
        gets(NUEVO[c].edad);

        printf("\nIngresa su sexo (hombre/mujer): ");
        gets(NUEVO[c].sexo);

        printf("\nInformacion de domicilio: ");
        printf("\nIngresa su calle: ");
        gets(NUEVO[c].calle);
        //scanf( "%s", NUEVO[c].calle);
        printf("\nIngresa su numero: ");
        gets(NUEVO[c].numero);      
        printf("\nIngresa su colonia: ");
        gets(NUEVO[c].colonia);
        printf("\nIngresa su Numero telefonco: ");
        gets(NUEVO[c].telefono);
    */   
        NUEVO[x].id=(x+1);
         printf("\nAlumno agregado!\n\n");
    }

}




void imprimeAlumnos(struct ALUMNO NUEVO[]){
//void imprimeAlumnos(){    
    int i=0;
    //system("cls");
    if (NUEVO[i].id>0){

        for(i;i<=ContGen;i++){
            printf("Alumno Numero %d \n",i);
            printf("Nombre: %s \n",NUEVO[i].nombre);
/*          printf("Edad: %s \n",NUEVO[i].edad);
            printf("Sexo: %s \n",NUEVO[i].sexo);
            printf("Direccion: ");
            printf("calle: %s ",NUEVO[i].calle);
            printf("calle: %s ",NUEVO[i].numero);
            printf("numero: %s \n",NUEVO[i].colonia);
            printf("Telefono: %s \n",NUEVO[i].telefono);
            printf("Carrera: %s \n",NUEVO[i].carrera);
*/          printf("\n\n");
        }
    }
    else {
        printf("\n No hay registros\n\n");
    }

}


void impNumeros(){
    printf("El numero de alumnos en Ing Industrial es:\t %d\n",contInd);
    printf("El numero de alumnos en Adm de Empresases:\t %d\n",contAdm);
    printf("El numero de alumnos en Psicologia es: \t %d\n",contPsi);
}
    
asked by Miguel Tlaseca 07.09.2018 в 20:10
source

2 answers

1

Do not use global variables

For more information see this other question:

Why using global variables is usually a bad idea?

Be careful that you are compiling in C ++

The compiler you are using is C ++, otherwise the program would give an error in these lines:

void agregarAlumno(ALUMNO);
void imprimeAlumnos(ALUMNO);

And the reason is that ALUMNO as such is nothing in C, you have to add struct :

void agregarAlumno(struct ALUMNO);
void imprimeAlumnos(struct ALUMNO);

or use typedef to create an alias and then save struct :

typedef struct {
    char nombre[30];
    char edad[2];
    char sexo[10];
    char calle[15];
    char numero[10];
    char colonia[15];
    char telefono[10];  
    int carrera;
    int id;
}ALUMNO;

void agregarAlumno(ALUMNO);
void imprimeAlumnos(ALUMNO);

The error

The error message is quite clear. You are trying to pass a pointer to a function that does not accept pointers but variables by value.

The problem gets a bit foggy because, since you have declared global variables, you have an overlap of variables:

struct ALUMNO{
    char nombre[30];
    char edad[2];
    char sexo[10];
    char calle[15];
    char numero[10];
    char colonia[15];
    char telefono[10];  
    int carrera;
    int id;
}NUEVO; // <<--- Primera declaracion de NUEVO

int contInd=0;
int contAdm=0;
int contPsi=0;
int ContGen=0;

void agregarAlumno(ALUMNO);
void imprimeAlumnos(ALUMNO);
void impNumeros();

int main(){
    struct ALUMNO NUEVO[9]; // <<--- Segunda declaracion de NUEVO

The fact is that the variable that affects you within main is the second one, which if you look at it is an array of 9 elements. However, the function requires a simple structure:

void agregarAlumno(ALUMNO);
void imprimeAlumnos(ALUMNO);

The solution is to modify these functions to accept an array:

void agregarAlumno(ALUMNO[]);
void imprimeAlumnos(ALUMNO[]);

In addition, you will get these statements to coincide with their respective implementations:

void agregarAlumno(struct ALUMNO NUEVO[]) {
//                                    ^^
    int indexCar;
    // ...
    
answered by 07.09.2018 в 21:11
1

There is a mixture of declared and used types of objects, which is what, in summary, is causing you the problem.

In particular, on your line 21 you declare that the addAlumn function receives an object (not an array) of type struct ALUMNO.

void agregarAlumno(ALUMNO); <-- Declarado como estructura simple

Then on line 44, as the error says ( 44 33 C: \ Users \ Miguel \ Desktop \ ss.cpp [Error] could not convert ' (STUDENT *) (& NEW) 'from' STUDENT * 'to' STUDENT ') indicates that you want to pass an array of STUDENT structures.

agregarAlumno(NUEVO);

Finally, in the implementation of the function on line 136, you change the declaration to indicate that it receives an array:

void imprimeAlumnos(struct ALUMNO NUEVO[]){ // <-- Implementado como Array

The solution to the problem is simple, the function must be declared and implemented in order to receive an array of structures. You must corroborate when you implement this type of functions that both parties are equal since the compiler controls the code as initially declared before arriving to review the implemented.


I pass your corrected code so you can try and verify this:

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

struct ALUMNO{
    char nombre[30];
    char edad[2];
    char sexo[10];
    char calle[15];
    char numero[10];
    char colonia[15];
    char telefono[10];  
    int carrera;
    int id;
};

int contInd=0;
int contAdm=0;
int contPsi=0;
int ContGen=0;

void agregarAlumno(struct ALUMNO []);
void imprimeAlumnos(struct ALUMNO []);
void impNumeros();

int main(){
    struct ALUMNO NUEVO[9];

    int op;
    do{
        //system("cls");
        printf("***MENU DE ESCUELA***\n\n");
        printf("1.- Agregar alumno.\n");
        printf("2.- Imprimir lista de alumnos.\n");
        printf("3.- Imprimir cifras de las carreras.\n");
        printf("4.- Salir.\n\n");
        printf("Seleccione su opcion: ");
        scanf("%d", &op);

        //system("cls");

        switch(op) {
            case 1:
                agregarAlumno(NUEVO);
            break;

            case 2:
                imprimeAlumnos(NUEVO);
            break;

            case 3:
                impNumeros();
            break;

            case 4:
                return 0;
            break;

            default:
                printf("Opcion invalida! ");
            break;
        }
    }while(op!=4);

        //system("Pause");

}


void agregarAlumno(struct ALUMNO NUEVO[]) {
    int indexCar;

    for (int x=0;x<9;x++){ // Corregí el rango ya que va de 0 a 8 (ya que son 9 valores los del array)
    printf("***AGREGAR NUEVO USUARIO***\n\n");
    printf("\n0 = Ingenieria Industrial \n1 = Administracion \n2 = Psicologia\n ");
    printf("\n\nIngresa el ID de la carrera al que ingresara: ");
    scanf("%d", &indexCar);
    NUEVO[x].carrera=indexCar;

    while(indexCar < 0 || indexCar-1 >= 2){
    printf("\nNo existe ninguna carrera con ese id!");
    printf("\n0 = Ingenieria Industrial \n1 = Administracion \n2 = Psicologia\n ");
    printf("\n\nIngresa el ID de la carrera al que ingresara: ");
    scanf("%d", &indexCar);
    }

    fflush(stdin);

    if  (NUEVO[x].carrera==0) 
    {contInd++;
    ContGen++;
    }
    if  (NUEVO[x].carrera==1) 
    {contAdm++;
    ContGen++;
    }

    if  (NUEVO[x].carrera==2) 
    {contPsi++;
    ContGen++;
    }

        fflush(stdin);

        printf("\nIngresa su nombre: ");
        //gets(NUEVO[c].nombre);
        //scanf( "%s", &NUEVO[c].nombre);
        scanf("%[^\n]s", NUEVO[x].nombre);

    /*  printf("\nIngresa su edad: ");
        gets(NUEVO[c].edad);

        printf("\nIngresa su sexo (hombre/mujer): ");
        gets(NUEVO[c].sexo);

        printf("\nInformacion de domicilio: ");
        printf("\nIngresa su calle: ");
        gets(NUEVO[c].calle);
        //scanf( "%s", NUEVO[c].calle);
        printf("\nIngresa su numero: ");
        gets(NUEVO[c].numero);      
        printf("\nIngresa su colonia: ");
        gets(NUEVO[c].colonia);
        printf("\nIngresa su Numero telefonco: ");
        gets(NUEVO[c].telefono);
    */   
        NUEVO[x].id=(x+1);
        printf("\nAlumno agregado!\n\n");
    }

}




void imprimeAlumnos(struct ALUMNO NUEVO[]){
//void imprimeAlumnos(){    
    int i=0;
    //system("cls");
    if (NUEVO[i].id>0){

        for(i;i<=ContGen;i++){
            printf("Alumno Numero %d \n",i);
            printf("Nombre: %s \n",NUEVO[i].nombre);
/*          printf("Edad: %s \n",NUEVO[i].edad);
            printf("Sexo: %s \n",NUEVO[i].sexo);
            printf("Direccion: ");
            printf("calle: %s ",NUEVO[i].calle);
            printf("calle: %s ",NUEVO[i].numero);
            printf("numero: %s \n",NUEVO[i].colonia);
            printf("Telefono: %s \n",NUEVO[i].telefono);
            printf("Carrera: %s \n",NUEVO[i].carrera);
*/          printf("\n\n");
        }
    }
    else {
        printf("\n No hay registros\n\n");
    }

}


void impNumeros(){
    printf("El numero de alumnos en Ing Industrial es:\t %d\n",contInd);
    printf("El numero de alumnos en Adm de Empresases:\t %d\n",contAdm);
    printf("El numero de alumnos en Psicologia es: \t %d\n",contPsi);
}

There are some good practices to be adjusted but I prefer to make this point clear of what you have consulted about your implementation so that you can strengthen that knowledge. I hope it helps you. Greetings!

    
answered by 07.09.2018 в 21:15