Created
May 17, 2024 02:00
-
-
Save diazvictor/4d0c4ca374fc707ec86e4c9285c09fed to your computer and use it in GitHub Desktop.
Este código es un ejemplo sencillo de validación de fechas en C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int main() { | |
// Declaración de variables | |
int month, day; | |
// Solicitar y almacenar el mes | |
cout << "Introduzca un mes (use 1 para Ene, etc.): "; | |
cin >> month; | |
// Validación del mes | |
if (month < 1 || month > 12 || floor(month) != month) { | |
cout << "Mes inválido. Debe ser un número entero entre 1 y 12." << endl; | |
return 1; | |
} | |
// Solicitar y almacenar el día | |
cout << "Introduzca un día del mes: "; | |
cin >> day; | |
// Validación del día | |
switch (month) { | |
case 1: case 3: case 5: case 7: case 8: case 10: case 12: | |
if (day < 1 || day > 31) { | |
cout << "Día inválido para el mes " << month << ". Debe ser entre 1 y 31." << endl; | |
return 1; | |
} | |
break; | |
case 2: | |
if (day < 1 || day > 28) { | |
cout << "Día inválido para el mes " << month << ". Debe ser entre 1 y 28." << endl; | |
return 1; | |
} | |
break; | |
case 4: case 6: case 9: case 11: | |
if (day < 1 || day > 30) { | |
cout << "Día inválido para el mes " << month << ". Debe ser entre 1 y 30." << endl; | |
return 1; | |
} | |
break; | |
default: | |
cout << "Mes inválido. Debe ser un número entre 1 y 12." << endl; | |
return 1; | |
} | |
// Si ambos son válidos, mostrar un mensaje de confirmación | |
cout << "Mes " << month << " y día " << day << " son válidos." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment