2
Below follows a code for date entry by the user and validation of it, but would like to modularize it. I don’t know if there is a possibility to use it outside of the box, or in an external file (e.g., "validation.h"). Can I get it leaner? Thanks in advance.
Edit1: In case, I will have to run this program in a "broker. c" which is based on function, and there are more tasks to be implemented in the code, so I would like it to stay as dry as possible, and the first thing that came into my mind was to exactly do the possible calculations and validations in an external file "lálálálá. h" and leave only the interaction in the main file "main. c".
Edit2: Exactly what the user Lacobus mentioned, was what I needed to understand to proceed with the development of the issues. Thank you!!
#include <stdio.h>
#include <stdlib.h>
struct DMA{
int dia;
int mes;
int ano;
};
typedef struct DMA dma;
int main(void)
{
dma data;
printf("Informe a data: ");
scanf("%d/%d/%d", &data.dia, &data.mes, &data.ano);
if(data.ano >= 10 && data.ano <= 9999){ //!checa o ano
if (data.mes >= 1 && data.mes <= 12){ //! checa o mes
if ((data.dia >= 1 && data.dia <= 31) && (data.mes == 1 || data.mes == 3 || data.mes == 5 || data.mes == 7 || data.mes == 8 || data.mes == 10 || data.mes == 12)){
puts("Data valida!"); //!checa os meses que tem 31 dias
}
else
if ((data.dia >= 1 && data.dia <= 30) && (data.mes == 4 || data.mes == 6 || data.mes == 9 || data.mes == 11)){
puts("Data valida!"); //!checa os meses que tem 30 dias
}
else if (data.dia >=1 && data.dia <= 28 && data.mes == 2){
puts("Data valida!"); //!valida as datas do mes 2
}
else if (data.dia == 29 && data.mes == 2 && (data.ano % 400 == 0 || (data.ano % 4 ==0 && data.ano % 100 != 0))){
puts("Data valida! (Ano bissexto)"); //!checa o ano bissexto e imprime informando
}
else{
puts("## ATENCAO: Dia invalido! ##"); //!informa caso DIA inserido for invalido
}
}
else{
puts("## ATENCAO: Mes invalido! ##"); //!informa caso MES inserido for invalido
}
}
else{
puts("## ATENCAO: Ano invalido! ##"); //!informa caso ANO inserido for invalido
}
return 0;
}
PS: The "!" in the comments is that I use Vscode and an extension that leaves comments colored with special characters.
Your question is how to separate the code into
.h
and.c
? If that’s the case not too long ago I answered an equal question: split-the-program-with-libraries-c-e-h– Isac
I took a look over it, and it seems to me to be what I need. I will look at it calmly now and try to run the code. Thanks @Isac !
– Adriel Gama
Possible duplicate of Split the program with libraries . c and . h
– Cleber Griff