Below is a code for user input and validation, but I would like to modularize it. I do not know if there is a possibility of using it out of the box, or in an external file (eg "validation.h"). Can I make him leaner? Thanks in advance.
Edit1: In this case, I will have to run this program in a "broker.c" that is function based, and there are more tasks to be implemented in the code, so I would like it to be as lean as possible, first thing that came into my mind was to do the possible calculations and validations in an external file "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 in order 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 the comments colorful with special characters.