Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What isthe correct syntax to handle a character or a keystoke that is not a number.

Posted on 2004-10-15
6
Medium Priority
?
234 Views
Last Modified: 2010-04-01
I am currently learning this languge, and I have a function that I would like to expand.
Current syntax:
void Circle::inputRadius()
{
    radius = -1;
    while (radius <= 0)
    {
            
        cout << "Please enter the radius of the circle: ";
        cin >> radius;
        // Error checking
        if (radius <= 0)
        {
            cout << endl << "Radius must be a number greater than zero."
                 << endl;
        }
    }
    cout << endl;

}
this takes care the possibility of the user entering a 0 or a - number as input.  This was what I was required to do for an assignment. however, I was wondering how to, or the correct syntax to handle (error check) a character or a keystoke that is not a number.? Thanks much ,Annie
0
Comment
Question by:bananaamy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12324911
>>(error check) a character or a keystoke that is not a number.?

You would have to use a string variable as your input variable, and then test to see if it's a number.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12324927
   char TestVar[33];

    radius = -1;
    while (radius <= 0)
    {
         
        cout << "Please enter the radius of the circle: ";

        cin >> TestVar;
        if (!isdigit(TestVar[0])
        {//Not a number, so report error here
            continue;
        }
        radius = atoi(TestVar);

        // Error checking
        if (radius <= 0)
        {
            cout << endl << "Radius must be a number greater than zero."
                 << endl;
        }
    }
    cout << endl;
0
 
LVL 30

Expert Comment

by:Axter
ID: 12324928
I'm not sure what type is radius.
If radius is a double or float, the you can use atof
radius = atof(TestVar);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:bananaamy
ID: 12324996
radius is a double,-
 where would be the best place to declare  "TestVar" -private ?
and.....
   if (!isdigit(TestVar[0]) //  this tests to see if input is a number??

radius = atoi(TestVar);   //     what  does atoi do?
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 12325614
>>if (!isdigit(TestVar[0]) //  this tests to see if input is a number??

Yes.  It test to see if the first character is a number.

>>radius = atoi(TestVar);   //     what  does atoi do?
atoi converts a string to a number.
It stands for Ascii-To-Integer (atoi)

If radius is a double, then you can use atof
radius = atof(TestVar);  

>>where would be the best place to declare  "TestVar" -private ?
It shouldn't be private, because you should not declare it as a member variable.
Instead declare it within the function you're using it in.
Furthermore, I recommend you declare it right before you use it in your code.
Example:
    while (radius <= 0)
    {
         
        cout << "Please enter the radius of the circle: ";

        char TestVar[33]; //  *****Declare it here*****
        cin >> TestVar;

        if (!isdigit(TestVar[0])
        {//Not a number, so report error here
            continue;
        }
        radius = atoi(TestVar);

        // Error checking
        if (radius <= 0)
        {
            cout << endl << "Radius must be a number greater than zero."
                 << endl;
        }
    }
0
 
LVL 15

Expert Comment

by:efn
ID: 12325636
Another way to do it is to check the fail bit of the cin stream.  If you try to read in a number and the stream chokes on the input, cin.fail() will become true.  Then you have to call cin.clear to reset the state and cin.ignore to read past the garbage before you try again.  For example:

            double d;
            cout << "Enter a number:\n";
            cin >> d;
            if (cin.fail())
            {
                  cout << "No good\n";
                  cin.clear();
                  cin.ignore(999, '\n');
            }

--efn
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question