|
-
Feb 22nd, 2002, 03:44 PM
#1
Thread Starter
Junior Member
Numeric or not??
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;
}
-
Feb 23rd, 2002, 12:05 PM
#2
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|