Solved

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

Posted on 2004-10-15
6
231 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

830 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