1

So.. I am a beginner and I was creating a really "basic" calculator, I thought about what should the program do if the input was a letter instead of a number, I would use a lot of IF/else statements, clear.cin , ignore.cin, every time one of the inputs was wrong. Afterwards, I thought I would make a function to automatically check if the input was an integar, and that's what came in mind first :

bool checkInputDouble (char x)

{

if (x>0 || x<0 || x==0)
 {return true;}

    else
    {return false;}

} 

I thought that these would apply to any number but not to a letter or any other character, however, it didn't work. After several trials, i found out the omitting the "x==0" condition would solve the problem, as SOMEHOW, the program thought of other characters as they equals zero. The function did its job and the calculator worked perfectly, but I thought that wasn't a practical solution, may be I will need to make a program where the user enters a number, but zero is an important input, I thought there must be like a statement or a function I don't know that would do the job, so any help ? can someone tell me what a function that REALLY does the job will look like ? and so for a function that checks for letters only, etc. Thanks in advance.

Jahid
  • 21,542
  • 10
  • 90
  • 108
Yosry
  • 147
  • 2
  • 9
  • 3
    Why not using [this](http://www.cplusplus.com/reference/cctype/isdigit/) ? What you are trying to do will always return true. – Blacktempel Jul 06 '15 at 05:08
  • You should worry about converting from a single character — maybe you meant to pass a string or character pointer. Assuming you want a string of some sort validated, consider `strtod()` and `strtol()` from ``, both of which tell you where they stopped converting, which allows you to validate that what was entered was indeed a valid number. – Jonathan Leffler Jul 06 '15 at 05:39
  • possible duplicate of [Check if the input is a number or string C++](http://stackoverflow.com/questions/21807658/check-if-the-input-is-a-number-or-string-c) – Robert Wadowski Jul 06 '15 at 05:49

3 Answers3

1

This function should work:

bool isDigit(char x){
   if (x>='0'&&x<='9')return true;
    else return false;}

You can also use the isdigit() function from cctype header:

#include <iostream>
#include <cctype>

int main(){
    std::cout<<isdigit('x')<<isdigit('0');
    return 0;}

Note: If you want a more generic solution which validates any type of input as double/integer, see this answer instead.

Community
  • 1
  • 1
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • can you explain to me this function ? it also doesn't work with negative numbers – Yosry Jul 06 '15 at 16:56
  • @Yosry How did you put a negative number in a char variable? The above function just takes a single character and checks if it is digit or not `(0-9)` – Jahid Jul 06 '15 at 17:49
  • @Yosry If you want a more generic solution which takes care of negative numbers, then see this answer instead: http://stackoverflow.com/a/31238989/3744681 – Jahid Jul 06 '15 at 17:52
0

Perhaps you can use the code from this answer.

double GetDouble() {
    double x;
    cin >> x;
    while (cin.fail() || (cin.peek() != '\r' && cin.peek() != '\n')) {
        cout << "Invalid Input! Please input a numerical value." << endl;
        cin.clear();
        while (cin.get() != '\n');
        cin >> x;
    }
    return x;
}

EDIT: Also use functions from numeric math like isnan(), isfinite(), isnormal(), etc.

Community
  • 1
  • 1
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
-1

You can use built in c++ isdigit funtion. In your code you are comparing character variable with integer so thats why you are getting something wrong.

You can write this like :

   bool checkInputDouble (char x)

    {

         if ( isdigit(x))
           return true;
         else
           return false;

    } 

or can use your own condition like :

bool checkInputDouble (char x)      
        {

             if ( x >='0' && x<='9')
               return true;
             else
               return false;
        }
Robin Halder
  • 253
  • 2
  • 14