Results 1 to 2 of 2

Thread: Numeric or not??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    California
    Posts
    25

    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;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width