elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  calcular funcion edad con funcion fecha de nacimiento
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: calcular funcion edad con funcion fecha de nacimiento  (Leído 24,294 veces)
WOLFMANX

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: calcular funcion edad con funcion fecha de nacimiento
« Respuesta #10 en: 27 Septiembre 2015, 23:58 pm »

#include <stdio.h>
#include <time.h>
 
typedef struct
{
    int dia;
    int mes;
    int año;
} fecha;
 
int calcular_edad(fecha datos)
{
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
 
    int a = ((tm.tm_year + 1900) * 100 + tm.tm_mon + 1) * 100 + tm.tm_mday;
    int b = (datos.año * 100 + datos.mes) * 100 + datos.dia;
 
    return (a - b) / 10000;
}
 
int main(void)
{
    fecha datos;
 
    printf("\nIngrese Fecha de Nacimiento dd/mm/aaaa: ");
    scanf("%d/%d/%d",&datos.dia,&datos.mes, &datos.año);
 
    printf("Su edad es %d\n", calcular_edad(datos));
 
    return(0);
}

me gustaria que me ayudaran, este codigo sirvio pero necesito solo utilizarlo con cout y cin por favor


En línea

WOLFMANX

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: calcular funcion edad con funcion fecha de nacimiento
« Respuesta #11 en: 28 Septiembre 2015, 00:00 am »

GRACIAS POR EL CODIGO PERO ME AYUDAN UTILIZANDO COUT Y CIN POR FAVOR. GRACIAS

Una solución a esto seria

Código
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. typedef struct
  5. {
  6.    int dia;
  7.    int mes;
  8.    int año;
  9. } fecha;
  10.  
  11. int calcular_edad(fecha datos)
  12. {
  13.    time_t t = time(NULL);
  14.    struct tm tm = *localtime(&t);
  15.  
  16.    int a = ((tm.tm_year + 1900) * 100 + tm.tm_mon + 1) * 100 + tm.tm_mday;
  17.    int b = (datos.año * 100 + datos.mes) * 100 + datos.dia;
  18.  
  19.    return (a - b) / 10000;
  20. }
  21.  
  22. int main(void)
  23. {
  24.    fecha datos;
  25.  
  26.    printf("\nIngrese Fecha de Nacimiento dd/mm/aaaa: ");
  27.    scanf("%d/%d/%d",&datos.dia,&datos.mes, &datos.año);
  28.  
  29.    printf("Su edad es %d\n", calcular_edad(datos));
  30.  
  31.    return(0);
  32. }
  33.  

Saludos


En línea

0xFer


Desconectado Desconectado

Mensajes: 400



Ver Perfil
Re: calcular funcion edad con funcion fecha de nacimiento
« Respuesta #12 en: 28 Septiembre 2015, 22:37 pm »

GRACIAS POR EL CODIGO PERO ME AYUDAN UTILIZANDO COUT Y CIN POR FAVOR. GRACIAS

Para usar cout y cin tienes que utilizar un espacio de nombres después de los includes pon using namespace std; y también #include <iostream>

Código
  1.  
  2.   cout << "Ingresa Fecha de Nacimiento" << endl;
  3.   cout << "Dia = ";
  4.   cin >> datos.dia;
  5.   .....
  6.  
  7.   cout << "Su edad es " << calcular_edad(datos);
  8.  
  9.  
En línea

Código
  1. int getRandomNumber(){
  2.    return 4; //chosen by fair dice roll
  3.              //guaranteed to be random
  4. }
Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Calcular potencia con una función recursiva
Programación C/C++
DickGumshoe 4 20,566 Último mensaje 8 Abril 2012, 18:24 pm
por DickGumshoe
Help! Calcular máximo de 3 numeros usando función
Programación C/C++
sora_ori 5 3,725 Último mensaje 18 Febrero 2013, 23:34 pm
por BatchianoISpyxolo
Función para calcular los números de Fibonacci
Programación C/C++
Caster 4 2,804 Último mensaje 18 Mayo 2013, 14:00 pm
por Caster
FUNCION PARA VALIDAR EDAD
Programación C/C++
carlosabcs18 6 3,738 Último mensaje 16 Julio 2014, 22:59 pm
por leosansan
EDAD A TRAVEZ DE FECHA DE NACIMIENTO
Programación C/C++
JEEM1296 0 1,972 Último mensaje 1 Diciembre 2015, 07:35 am
por JEEM1296
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines