I have made the following program that calculates the inheritance based on the number of children:
#include <iostream>
using namespace std;
int leer_datoi()
{
int dato;
cin >> dato;
return dato;
}
double leer_datod()
{
double dato;
cin >> dato;
return dato;
}
void calcular_herencia()
{
double herencia, mayor;
int num_hijos;
do{
cout << "Ingrese el importe de la herencia:\t";
herencia=leer_dato_d();
cout << "\n";
if(herencia<=0)
cout << "ERROR. Ingrese una cantidad positiva\n" << endl;
} while(herencia<=0);
do{
cout << "Ingrese la cantidad de hijos:\t\t";
num_hijos=leer_dato_i();
cout << "\n";
if(num_hijos<=0)
cout << "ERROR. Ingrese una cantidad positiva\n" << endl;
} while(num_hijos<=0);
if(num_hijos<=4)
{
herencia=herencia/num_hijos;
cout << "La herencia para cada hijo es:\t\t" << herencia << "\n" << endl;
}
else
{
mayor=herencia/2;
herencia=mayor/(num_hijos-1);
cout << "La herencia para el hijo mayor es:\t" << mayor << "\n" << endl;
cout << "La herencia para el resto de hijos es:\t" << herencia << "\n" << endl;
}
}
int main()
{
calcular_herencia();
return 0;
}
I tried it and it works normal. Then, out of curiosity I put a non-integer value when entering the number of children (let's say 4.5) and likewise the program worked, which seems odd to me since the variable num_hijos
is of integer type.
My question is:
Why is this?