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 the program still worked, which seems strange to me since the variable num_hijos
is of integer type.
My question is:
What is this about?
When the program encounters this:
Attempts to read an integer from standard input. If the first character encountered is a numeric digit or the sign (
+
or-
),stream
it will understand that it can start extracting an integer. When it ends? Since it's reading an integer it will end when it can't extract any more digits, which happens, in your case, when it meets point. At that moment it stops reading and returns you todato
the number read up to that moment.That is, before an input
4.5
,dato
a is stored4
in and the residue remains in the input buffer.5
. You can check that this is true with the following example:When faced with a type input
4.5
, the program prints the following:So, as you can see, the program doesn't do any magic, it just gives you what you ask for.
We would have another different situation if we did the conversion once the data has been read. For example, imagine that instead of reading an integer, you read a
double
and then try to store that number in an integer. Something like that:In this case it
cin
will read the entire number4.5
and store it intemporal
. Converting to integer will cause decimals to be truncated, so the decimal part (.5
) will be lost anddato
the integer part ( ) will eventually be stored in4
.Where is the difference in both cases? The result stored in
dato
does not change, it is the same in both cases, however in this second example the input buffer is left clean, with no residue.If we modify the example a bit:
And you enter, for example,
4.5 6
the program will print the following:Where does that 0 come from? What happens is that after reading the first integer in the input buffer, the following residue remains:
.5 6
. That initial point prevents you from correctly reading a whole second. When the reading error occurs, the error flags are activatedcin
and the stream is blocked until said flags are reset.This example shows how important it is to clear the input buffer before performing a read:
Where:
clear
clears the error flags of the stream . This action is only executed when reading the first integer fails (for example if you enter a character).ignore
allows you to discard characters from the stream . In this case, all characters found up to the first line break will be discarded.numeric_limits
is a class that allows obtaining the limit values for each type of data (remember that the ranges are usually dependent on the machine). In this case it gets the largest number that can be stored in a signed integer.The program, when capturing the input data through, will
cin
effectively process the data as a double precision type. When assigning to the local variabledato
in the functionleer_datoi
, the fractional part will be truncated and only the integer part will be taken.You can do the following test:
You should see here a 4 as screen output
Integer type variables store integers. If you enter any variable with a comma, it saves the integer. This is useful in many cases to break down integers... Eg: 17022017 is a ddmmyyyy type date, if we want to save the day we define: Int day = 17022017 / 1000000 Which gives us the result 02.172017... but when saving only the integer then day == 17