PDA

Click to See Complete Forum and Search --> : Numeric or not??


KMMATH
Feb 22nd, 2002, 02:44 PM
I am asking the user to enter a number and want to display an error message and ask again if the user incorrectly enters an alpha character.
These work with the correct input (numeric), but won't work with the wrong input (alpha)
Please help

void get_loan_amount(double& lamount)
{
using namespace std;
do
{
cout << "Please enter the loan amount: $";
cin >> lamount;
} while (isalpha(lamount));
return;
}

OR

void get_loan_amount(double& lamount)
{
using namespace std;
double amount;
do
{
cout << "Please enter the loan amount: $";
cin >> amount;
lamount = amount;
} while (!isdigit(amount));
return;
}

CornedBee
Feb 23rd, 2002, 11:05 AM
is... functions work only on single characters. cin makes sure that non-digits are ignored when reading a double (that's the great thing about the overloaded stream operators).

If the user enters invalid input here (like "foo"), the result is 0.0
If the user enters "23.4whoa" the result is 23.4