Hello sir..
i am a beginner in C++..
how to determine if the input is not an integer..
thnx in advance..
Hello sir..
i am a beginner in C++..
how to determine if the input is not an integer..
thnx in advance..
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
#include <cctype> has a function for that called isdigit(). Here's an example of it.
More information about this header can be further read here:Code:if(isdigit(a)) cout << "digit" << endl; else cout << "character" << endl;
cctype (ctype.h) - C++ Reference
You can also just attempt to input a number and test whether this returns true. Like
if(cin>>a)
note: It doesn't really return true. It will just evaluate to true.
Code:cin>>input; for(int x=0; x<strlen(input); x++) { if( (int) input[x] < (int) '0' || (int) input[x] > (int) '9' ) ) { cout<<"Not a numerical input"; break; } }
like_no_other, you should indent your code properly. It would help if you actually showed the declaration of input (and it looks like your code will be vulnerable to buffer overflow). Chances are, all that casting is unnecessary and the check is more descriptively done with isdigit() anyway.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, you're right, it is faulty.He should definately use isdigit() , but I think he could learn more by giving him an explicit way to check his input other than "i_do_all_the_work_for_you" function.
Code:char input[20]; cin.getline(input, 19, '\n'); bool IsInt=1; for(int x=0; x<strlen(input); x++) { if( (int) input[x] < (int) '0' || (int) input[x] > (int) '9' ) ) { IsInt=0; break; } }
That would be better written as:
EDIT:Code:char input[20]; cin.getline(input, sizeof(input), '\n'); bool IsInt = true; for (size_t x = 0, n = strlen(input); x < n; ++x) { if (input[x] < '0' || input[x] > '9') { IsInt = false; break; } }
Ah, but I notice that this algorithm does not correctly handle the input of an empty string.
Last edited by laserlight; 05-08-2009 at 12:14 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> He should definately use isdigit()
I disagree. The operator>> used with cin automatically determines if input is an integer or not. There's no need for you to re-implement what was already done.
Won't just cin.fail() work if the variable is defined as an integer?
It seems to me that we now have x == 1 iff the user didn't enter a valid integer. And you need no additional library or anything.Code:int Var; bool x; cin << Var; x = cin.fail();
Yes, that's what we're talking about when we say to use if(cin>>a). The return value of cin >> a evaluates to false if an integer was not read in (and the failbit is set).
@laserlight:i don't follow, what was wrong with my code?
Originally Posted by like_no_other
- If the input is an empty string, your code would treat it as a valid integer.
- getline() should have been called with 20 instead of 19 as the second argument (but then 20 was arbitrary in the first place, so this is a minor point).
- strlen() should only be called once rather than on each iteration.
- The casts to int are unnecessary.
- The code needs to be properly and consistently indented.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> what was wrong with my code?
Beyond what laserlight stated, your code sets IsInt to false on valid integers and true on invalid integers. True, those cases (negative numbers or overflow) might be rare depending on what you're using it for, but you don't know what the OP is using this for so I'd recommend the built-in capabilities that handle end cases better.
thnx to all of u guys.. and sori for the late reply... i used "if (cin>>a)" and it works but when i enter any number that has a decimal like 2.35 or 13.43 etc.. it treats as an integer too why? AFAIK ther is no decimal in integer value..
thnx in advance and sori for my english..