Problems with C libraries

2

I have this library to perform different validations in my project

#ifndef VALIDACION_H
#define VALIDACION_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include <ctype.h>
#include <stdbool.h>
//#include "listaMaterias.h"


bool validarNumero(char numero[50]);
bool validarLetras(char nombre[50]);
bool validarFecha(char dia[], char mes[], char ano[]);
//bool validarMateria(_nodoMaterias *apuntadorMaterias, char materia[20]);

bool validarNumero(char numero[])
{
    int i = 0, j;

    j = strlen(numero);

    while (i < j)
    {
        if (isdigit(numero[i]) != 0)
        {
            i++;
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool validarLetras(char nombre[])
{
    int i = 0, j;

    j = strlen(nombre);

    while (i < j)
    {
        if (isalpha(nombre[i]) != 0)
        {
            i++;
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool validarFecha(char dia[], char mes[], char ano[])
{
    int month, day, year;

        day = atoi(dia);
        month = atoi(mes);
        year = atoi(ano);

    //Si el mes ingresado es Febrero, el año no es bisiesto y el día es mayor a 28 o menor o igual a cero
    if((month == 2) && !((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && ((day > 28) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 28 dias");
        return false;
    }
    //Si el mes ingresado es Febrero, el año es bisiesto y el día es mayor a 29 o menor o igual a cero
    else if((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && ((day > 29) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 29 dias");
        return false;
    }
    //Si el mes es distinto a Febrero (cualquiera de los demás meses que tengan 31 días) y el día es mayor a 31 o menor o igual a cero
    else if(((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && ((day > 31) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 31 dias");
        return false;
    }
    //Si el mes es distinto a Febrero (cualquiera de los demás meses que tengan 30 días) y el día es mayor a 30 o menor o igual a cero
    else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day > 30) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 30 dias");
        return false;
    }
    //Si el mes es mayor a 12 o menor o igual a cero
    else if((month > 12) || (month <= 0))
    {
        printf("\n\nFecha incorrecta: El año solo tiene 12 meses");
        return false;
    }

    return true;
}

/*bool validarMateria(_nodoMaterias *apuntadorMaterias, char materia[20])
{
    _nodoMaterias *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntadorMaterias;

    while(apuntadorAuxiliar != NULL)
    {
        if (apuntadorAuxiliar != NULL && strcmp(apuntadorAuxiliar->nombre, materia) != 0)
        {
            return false; //SI ENTRA EN ESTE IF SIGNFICA QUE LA MATERIA QUE INTRODUJO NO EXISTE EN LA LISTA DE MATERIA QUE TIENE
        }

        apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
    }

    return true;
}*/

#endif //VALIDACION_H 

Library "listaMaterias.h"

#ifndef LISTAMATERIAS_H
#define LISTAMATERIAS_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "validacion.h"

struct materias
{
    char nombre[30];
    char profesor[30];
    char tipoDeMateria[20];
    char horasSemanales[10];
    struct materias *siguiente;
};

typedef struct materias _nodoMaterias;

_nodoMaterias *crearListaMaterias(_nodoMaterias *apuntador);
bool listaVacia(_nodoMaterias *apuntador);
_nodoMaterias *insetarEnListaMaterias(_nodoMaterias *apuntador);
void imprimirLista (_nodoMaterias *apuntador);
_nodoMaterias *crearNodoMaterias(char nombre[], char profesor[], char tipoDeMateria[], char horasSemanales[]);
_nodoMaterias *eliminarMateria(_nodoMaterias *apuntador);
bool buscarMateria(char nombre[], _nodoMaterias *apuntador);
_nodoMaterias *modificarMateria(_nodoMaterias *apuntador);
bool imprimirListaDeMaterias(_nodoMaterias *apuntador);

//AQUI SE CREA LISTA Y SE PONE PARA QUE APUNTE A NULL
_nodoMaterias *crearListaMaterias(_nodoMaterias *apuntador)
{
    return (apuntador = NULL);
}

//ESTA FUNCION VERIFICA SI LA LISTA ESTA VACIA 
bool listaVacia(_nodoMaterias *apuntador)
{
    if (apuntador == NULL)
        return (true); //SI SALE EL TRUE SIGNIFICA QUE LA LISTA ESTA VACIA
    else
        return (false);//SI SALE EL FALSE SIGNIFICA QUE LA LISTA NO ESTA VACIA 
}

//AQUI SE CREA EL NUEVO NODO DE LA LISTA
_nodoMaterias *crearNodoMaterias(char nombre[], char profesor[], char tipoDeMateria[], char horasSemanales[])
{
    _nodoMaterias *registroNuevo;

    registroNuevo = (_nodoMaterias *) malloc(sizeof(_nodoMaterias));

    system("clear");
    printf("\n----NUEVA MATERIA----\n");
    printf("NOMBRE: ");
    fflush(stdin);
    scanf("%s",nombre);
    while(!validarLetras(nombre))
    {
        printf("\nPOR FAVOR SOLO ESCRIBA LETRAS ");
        printf("\nNOMBRE: ");
        fflush(stdin);
        scanf("%s",nombre);

        /*while(!buscarMateria())*/
    }
    printf("PROFESOR: ");
    fflush(stdin);  
    scanf("%s",profesor);
    while(!validarLetras(profesor))
    {
        printf("\nPOR FAVOR SOLO ESCRIBA LETRAS ");
        printf("\nPROFESOR: ");
        fflush(stdin);
        scanf("%s",profesor);
    }
    printf("TIPO DE MATERIA: ");
    fflush(stdin);  
    scanf("%s",tipoDeMateria);
    while(!validarLetras(tipoDeMateria))
    {
        printf("\nPOR FAVOR SOLO ESCRIBA LETRAS");
        printf("\nTIPO DE MATERIA: ");
        fflush(stdin);
        scanf("%s",tipoDeMateria);
    }
    printf("HORAS SEMANALES:  ");
    fflush(stdin);
    scanf("%s",horasSemanales);
    while(!validarNumero(horasSemanales))
    {
        printf("\nPOR FAVOR SOLO ESCRIBA NUMEROS");
        printf("\nHORAS SEMANALES: ");
        fflush(stdin);
        scanf("%s",horasSemanales);
    }
    fflush(stdin);

        strcpy(registroNuevo->nombre, nombre);
        strcpy(registroNuevo->profesor, profesor);
        strcpy(registroNuevo->tipoDeMateria, tipoDeMateria);
        strcpy(registroNuevo->horasSemanales, horasSemanales);
        registroNuevo->siguiente = NULL;

    return registroNuevo;   

}

//AQUI SE INSERTA EL NODO EN LA LISTA LUGEO DE SER CREADO POR LA FUNCION crearNodo
_nodoMaterias *insetarEnListaMaterias(_nodoMaterias *apuntador)
{
    _nodoMaterias *registroNuevo, *apuntadorAuxiliar;
    char respuesta,ch;
    char nombre[30];
    char profesor[30];
    char tipoDeMateria[20];
    char horasSemanales[10];

    //ESTE CICLO SE ENCARGA DE QUE SE REPITA EL PORCESO PARA PODER INGRESAR MATERIAS HASTA QUE EL USUARIO DECIDA
    do
    {
            registroNuevo = crearNodoMaterias(nombre, profesor, tipoDeMateria, horasSemanales);
            if (listaVacia(apuntador)) 
                apuntador = registroNuevo;
            else
            {
                apuntadorAuxiliar = apuntador;
                while (apuntadorAuxiliar->siguiente != NULL)
                    apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
                apuntadorAuxiliar->siguiente = registroNuevo;
            }

            printf("\nPARA INGRESAR OTRA MATERIA MARQUE... 1");
            printf("\nPARA SALIR MARQUE... '0'\n");         
     while((ch = getchar()) != EOF && ch != '\n');      

    scanf("%c", &respuesta);
        fflush(stdin);          
        while((ch = getchar()) != EOF && ch != '\n');

    }while (respuesta == '1');
    return apuntador;
}

//TRATAR DE QUE ELIMINE TODOSO LOS NODOS QUE CONCUERDEN CON EL CAMPO BUSCADO
_nodoMaterias *eliminarMateria(_nodoMaterias *apuntador)
{
    char materia[20];
    system("clear");
    printf("\nQUE MATERIA DESEA ELIMINAR:  ");
    scanf("%s",materia);
    fflush(stdin);

    //INTENTO DE BORRAR NODO 1
    if (!listaVacia(apuntador))
    {
        _nodoMaterias *borrarAuxiliar;
        _nodoMaterias *anterior = NULL;

        borrarAuxiliar = apuntador;


            while (borrarAuxiliar != NULL && strcmp(borrarAuxiliar->nombre, materia) != 0) 
            {
                anterior = borrarAuxiliar;
                borrarAuxiliar = borrarAuxiliar->siguiente;
            }

            if (borrarAuxiliar == NULL)
            {
                printf("\nNODO NO ENCONTRADO");
            }else if (anterior == NULL)
                {
                    apuntador = apuntador->siguiente;
                    free(borrarAuxiliar);               
                } else
                    {
                        anterior->siguiente = borrarAuxiliar->siguiente;
                        free(borrarAuxiliar);                   
                    }

        /*while (apuntador != NULL)
        {
            /*anterior = borrarAuxiliar;
            borrarAuxiliar = borrarAuxiliar->siguiente;*/
            /*if (apuntador != NULL && strcmp(apuntador->nombre, materia) == 0)
            {
                anterior->siguiente = apuntador->siguiente;
                borrarAuxiliar = apuntador;
                apuntador = apuntador->siguiente;
                free(borrarAuxiliar);
            }
            else
            {
                anterior = apuntador;
                apuntador = apuntador->siguiente;
            }
        }*/
    printf("\nSU MATERIA FUE BORRADA EXITOSAMENTE");
    getchar();  
    }
    else
    {
        printf("\nNO EXISTE ESA MATERIA CREADA");
        getchar();
    }

    return apuntador;
}

_nodoMaterias *modificarMateria(_nodoMaterias *apuntador)
{
    char materia[20];
    system("clear");
    printf("\nQUE MATERIA DESEA MODIFICAR:  ");
    scanf("%s",materia);
    fflush(stdin);

    if (!listaVacia(apuntador))
    {
        _nodoMaterias *apuntadorAuxiliar;

        apuntadorAuxiliar = apuntador;

        while (apuntadorAuxiliar != NULL)
        {           
            if (apuntadorAuxiliar != NULL && strcmp(apuntadorAuxiliar->nombre, materia) == 0)
            {
                char nombre[30];
                char profesor[30];
                char tipoDeMateria[20];
                char horasSemanales[10];

                printf("\nINGRESE LOS NUEVOS DATOS DE LA MATERIA");
                printf("\nNOMBRE: ");
                fflush(stdin);
                scanf("%s",nombre);
                while(!validarLetras(nombre))
                {
                    printf("\nPOR FAVOR SOLO ESCRIBA LETRAS ");
                    printf("\nNOMBRE: ");
                    fflush(stdin);
                    scanf("%s",nombre);
                }
                printf("PROFESOR: ");
                fflush(stdin);  
                scanf("%s",profesor);
                while(!validarLetras(profesor))
                {
                    printf("\nPOR FAVOR SOLO ESCRIBA LETRAS ");
                    printf("\nPROFESOR: ");
                    fflush(stdin);
                    scanf("%s",profesor);
                }
                printf("TIPO DE MATERIA: ");
                fflush(stdin);  
                scanf("%s",tipoDeMateria);
                while(!validarLetras(tipoDeMateria))
                {
                    printf("\nPOR FAVOR SOLO ESCRIBA LETRAS");
                    printf("\nTIPO DE MATERIA: ");
                    fflush(stdin);
                    scanf("%s",tipoDeMateria);
                }
                printf("HORAS SEMANALES:  ");
                fflush(stdin);
                scanf("%s",horasSemanales);
                while(!validarNumero(horasSemanales))
                {
                    printf("\nPOR FAVOR SOLO ESCRIBA NUMEROS");
                    printf("\nHORAS SEMANALES: ");
                    fflush(stdin);
                    scanf("%s",horasSemanales);
                }
                fflush(stdin);

                strcpy(apuntadorAuxiliar->nombre, nombre);
                strcpy(apuntadorAuxiliar->profesor, profesor);
                strcpy(apuntadorAuxiliar->tipoDeMateria, tipoDeMateria);
                strcpy(apuntadorAuxiliar->horasSemanales, horasSemanales);
                apuntadorAuxiliar->siguiente = NULL;
            }

            apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
        }
    printf("\nSU MATERIA FUE MODIFICADA EXITOSAMENTE");
    getchar();  
    }
    else
    {
        printf("\nNO EXISTE ESA MATERIA CREADA");
        getchar();
    }

    return apuntador;
}

//IMPRIMIR LOS NODOS DE LA LISTA
void imprimirLista (_nodoMaterias *apuntador)
{
    _nodoMaterias *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntador;

    if (apuntadorAuxiliar == NULL)
        printf("NO HAY ELEMENTOS EN LA LISTA \n");
    else
    {
        while(apuntadorAuxiliar != NULL)
        {
            printf(" \n------------MATERIAS-------------- ");
            printf("\nNOMBRE: %s \n", apuntadorAuxiliar->nombre);
            printf("\nPROFESOR: %s \n", apuntadorAuxiliar->profesor);
            printf("\nTIPO DE MATERIA: %s \n", apuntadorAuxiliar->tipoDeMateria);
            printf("\nHORAS SEMANALES: %s \n", apuntadorAuxiliar->horasSemanales);

            apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
        }
    }

    return;
}

//ESTA FUNCION LE PERMITE SABER AL USUARIO LAS MATERIAS QUE INSCRIBIO 
bool imprimirListaDeMaterias(_nodoMaterias *apuntador)
{
    int contador;

    _nodoMaterias *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntador;

    if (apuntadorAuxiliar == NULL)
    {
        printf("\n\n          NO HAY ELEMENTOS EN LA LISTA\n");
        return false;
    }
    else
    { 
        contador = 0;
        printf("\nLISTA DE MATERIAS REGISTRADAS\n"); //AQUI VAN APARECIENDO LAS MATERIAS EN ORDEN ASI EL USARIO SABE CUALES DEBE SELCCIONAR
        while(apuntadorAuxiliar != NULL)
        {
            contador++;
            printf("%d", contador);printf(".- %s \n", apuntadorAuxiliar->nombre);

            apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
        }
    }

    return true;    
}

bool buscarMateria(char materia[], _nodoMaterias *apuntador)
{
    _nodoMaterias *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntador;

    while (apuntadorAuxiliar != NULL)
    {
        if (strcmp(apuntadorAuxiliar->nombre, materia) == 0)
        {
            break;
        }
        apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
    }

    if (apuntadorAuxiliar == NULL)
        return true;
    else
        return false;


}

#endif //LISTAMATERIAS_H

To use the function bool validarMateria(_nodoMaterias *apuntadorMaterias, char materia[20]); I have to call the library "listaMaterias.h" to be able to use the data type _nodoMaterias but when I compile the code I get the following errors

In file included from validacion.h:10:0,
                 from usuario.h:8,
                 from menu.c:4:
listaMaterias.h: In function ‘crearNodoMaterias’:
listaMaterias.h:58:9: warning: implicit declaration of function ‘validarLetras’ [-Wimplicit-function-declaration]
  while(!validarLetras(nombre))
         ^
listaMaterias.h:90:12: warning: implicit declaration of function ‘validarNumero’ [-Wimplicit-function-declaration]
     while(!validarNumero(horasSemanales))
            ^
In file included from usuario.h:8:0,
                 from menu.c:4:
validacion.h: At top level:
validacion.h:13:6: error: conflicting types for ‘validarNumero’
 bool validarNumero(char numero[50]);
      ^
In file included from validacion.h:10:0,
                 from usuario.h:8,
                 from menu.c:4:
listaMaterias.h:90:12: note: previous implicit declaration of ‘validarNumero’ was here
     while(!validarNumero(horasSemanales))
            ^
In file included from usuario.h:8:0,
                 from menu.c:4:
validacion.h:14:6: error: conflicting types for ‘validarLetras’
 bool validarLetras(char nombre[50]);
      ^
In file included from validacion.h:10:0,
                 from usuario.h:8,
                 from menu.c:4:
listaMaterias.h:58:9: note: previous implicit declaration of ‘validarLetras’ was here
  while(!validarLetras(nombre))

I do not know why I get these errors, I assume it's because in the library "listaMaterias.h" I use functions declared in the validation library but I want to know how I can solve this.

    
asked by ilfredo 01.12.2016 в 21:07
source

1 answer

2

You should not place your functions validateNumber, validateLetras and validateDate in the .h file, you just have to place them in the .c file

The definition of the data type _nodoMaterias, you also have to place it in the .h

YOUR FILES SHOULD REMAIN IN THE FOLLOWING WAY:

validacion.c

#include "validacion.h"

bool validarNumero(char numero[])
{
    int i = 0, j;

    j = strlen(numero);

    while (i < j)
    {
        if (isdigit(numero[i]) != 0)
        {
            i++;
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool validarLetras(char nombre[])
{
    int i = 0, j;

    j = strlen(nombre);

    while (i < j)
    {
        if (isalpha(nombre[i]) != 0)
        {
            i++;
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool validarFecha(char dia[], char mes[], char ano[])
{
    int month, day, year;

        day = atoi(dia);
        month = atoi(mes);
        year = atoi(ano);

    //Si el mes ingresado es Febrero, el año no es bisiesto y el día es mayor a 28 o menor o igual a cero
    if((month == 2) && !((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && ((day > 28) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 28 dias");
        return false;
    }
    //Si el mes ingresado es Febrero, el año es bisiesto y el día es mayor a 29 o menor o igual a cero
    else if((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && ((day > 29) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 29 dias");
        return false;
    }
    //Si el mes es distinto a Febrero (cualquiera de los demás meses que tengan 31 días) y el día es mayor a 31 o menor o igual a cero
    else if(((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && ((day > 31) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 31 dias");
        return false;
    }
    //Si el mes es distinto a Febrero (cualquiera de los demás meses que tengan 30 días) y el día es mayor a 30 o menor o igual a cero
    else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day > 30) || (day <= 0)))
    {
        printf("\n\nFecha incorrecta: Este mes solo tiene 30 dias");
        return false;
    }
    //Si el mes es mayor a 12 o menor o igual a cero
    else if((month > 12) || (month <= 0))
    {
        printf("\n\nFecha incorrecta: El año solo tiene 12 meses");
        return false;
    }

    return true;
}

bool validarMateria(_nodoMaterias *apuntadorMaterias, char materia[20])
{
    _nodoMaterias *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntadorMaterias;

    while(apuntadorAuxiliar != NULL)
    {
        if (apuntadorAuxiliar != NULL && strcmp(apuntadorAuxiliar->nombre, materia) != 0)
        {
            return false; //SI ENTRA EN ESTE IF SIGNFICA QUE LA MATERIA QUE INTRODUJO NO EXISTE EN LA LISTA DE MATERIA QUE TIENE
        }

        apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
    }

    return true;
}

validacion.h

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

struct materias
{
    char nombre[30];
    char profesor[30];
    char tipoDeMateria[20];
    char horasSemanales[10];
    struct materias *siguiente;
};

typedef struct materias _nodoMaterias;

bool validarNumero(char numero[50]);
bool validarLetras(char nombre[50]);
bool validarFecha(char dia[], char mes[], char ano[]);
bool validarMateria(_nodoMaterias *apuntadorMaterias, char materia[20]);
    
answered by 01.12.2016 в 21:13