Solved

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

Posted on 2004-10-15
6
230 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 61
C Language combined operators 28 109
object oriented programming comparison 5 71
VS2015 Redefinition errors 4 51
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

770 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