but I noticed that from a certain number of figures, the program ended up throwing only the error line.
The numbers are stored in b
, which is type int
. The type int
is a type of signed whole data. This type of data, 32 bits, uses a bit to store the sign and the remaining 31 to store the number itself.
That is, for 31 bits, the largest number it can support is: 2 ^ 31-1 = 2,147,483,647.
If you try to enter a larger number, cin
will not be able to store it in the variable and this causes an error flag to be activated ... from that moment cin
is blocked until you deal with the error.
The most usual thing in these cases is to clean the error flag and empty the input buffer:
while (b > 9999 || b < 1110) {
std::cout << "\nE R R O R. Favor de ingresar un numero de cuatro digitos.\n";
std::cin >> b;
if( std::cin.fail() )
{
std::cin.ignore(std::numeric_limits<int>::max(),'\n');
std::cin.clear();
}
}
Where:
-
numeric_limits
is a C ++ template that gives you information about the numeric types. In this case we are asking you for the highest value that can be stored in a variable of type int
. numeric_limits
is in the library limits
.
-
cin.ignore
discards input characters ... in this case it will discard everything there is until it encounters a line break (which will also be discarded). The first parameter indicates the number of bytes to be discarded ... hence we use numeric_limits
... we want to discard the maximum possible.
-
cin.clear
resets the error flag, which makes cin
available again to read new values.
EDITO
The problem with the previous answer is that it does not contemplate the case that an incorrect entry is entered in the first iteration. I do not like to repeat code, so a possible solution would be the following:
int main()
{
int b;
bool pedirNumero = true;
while(pedirNumero)
{
std::cin >> b;
pedirNumero = std::cin.fail() || b > 9999 || b < 1110;
if( pedirNumero )
{
std::cout << "\nE R R O R. Favor de ingresar un numero de cuatro digitos.\n";
std::cin.ignore(std::numeric_limits<int>::max(),'\n');
std::cin.clear();
}
}
std::cout << b;
}