string fecha;
cout<<"Ingresa Su fecha de nacimiento: "<<endl;
cin>>fecha;
cout<<"tu edad es: "<<endl;
cin>>fecha-2017;
In the last line you have two errors:
- date is of
string
, which does not support arithmetic operations
- you are using
cin
instead of cout
To solve the first problem you must convert the date into a numerical representation, in such a way that it is possible to perform arithmetic operations with it. To be able to do this before you have to be clear that you must specify what will be the expected format of the date:
- dd / mm / YYYY
- mm / dd / YYYY
- YYYY / mm / dd
- ??
Once you have specified the format you can start processing the date. Let's assume that you have chosen the first option. A simple way to access it would be:
int ExtraerAnio(std:: string const& fecha)
{
int dia,mes,anio;
std::sscanf(fecha.c_str(), "%d/%d/%d", &dia, &mes, &anio) != 3)
{
// Error de parseo
}
return anio;
}
With what your code would be
cout<<"tu edad es: "<<endl;
cout>>ExtraerAnio(fecha)-2017;
The code could be improved to take into account if you are not yet years old or not taking into account the day and month