Go Premium for a chance to win a PS4. Enter to Win

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
?
235 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

927 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