calcular funcion edad con funcion fecha de nacimiento

Iniciado por dragonlady, 10 Julio 2015, 21:40 PM

0 Miembros y 1 Visitante están viendo este tema.

WOLFMANX

#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

WOLFMANX

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

Cita de: Coper en 11 Julio 2015, 06:12 AM
Una solución a esto seria

Código (cpp) [Seleccionar]
#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);
}


Saludos

0xFer

Cita de: WOLFMANX en 28 Septiembre 2015, 00:00 AM
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 (cpp) [Seleccionar]


   cout << "Ingresa Fecha de Nacimiento" << endl;
   cout << "Dia = ";
   cin >> datos.dia;
   .....

   cout << "Su edad es " << calcular_edad(datos);

Código (java) [Seleccionar]
int getRandomNumber(){
    return 4; //chosen by fair dice roll
              //guaranteed to be random
}