Solved

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

Posted on 2004-10-15
6
228 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now