I've been trying to understand how to dynamically allocate array space in my free time and I've started to work on a simple program that will store temperatures in an array that I will dynamically create space for.I've g
int GetValidInteger(constint MIN, constint MAX)
{
double validNumber = 0.0; // holds the user input
validNumber = GetValidDouble(MIN, MAX); // Get user input as a double
if(validNumber > (int)validNumber) // If user input is not a whole number
{
// report the problem to the user.
cerr << "\nInvalid input. Please try again and enter whole number.\n";
validNumber = GetValidInteger(MIN, MAX); // try again using recursion.
}
return (int) validNumber; // returns a valid value to the calling function.
}
// GetValidDouble function definition
double GetValidDouble(constdouble MIN, constdouble MAX)
{
double validNumber = 0.0; // holds the user input
cin >> validNumber; // try to get input
if(cin.fail()) // if user input fails...
{
// reset the cin object and clear the buffer.
ClearInputBuffer();
// report the problem to the user.
cerr << "\nInvalid input. Please try again and enter a numeric value.\n";
// Try again by calling the function again (recursion)
validNumber = GetValidDouble(MIN, MAX);
}
elseif(validNumber < MIN || validNumber > MAX)// if value is outside range...
{
// report the problem to the user.
cerr << "\nInvalid input. Please try again and enter a value between "
<< MIN << " and " << MAX << ".\n";
// Try again by call the function again (recursion)
validNumber = GetValidDouble(MIN, MAX);
}
return validNumber; // returns a valid value to the calling function.
}