Whhat is wrong with this isdigit
I just wanted to see whether it is a digit or not but it gave a night of headache
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
int a;
cin>>a;
while(!isdigit(a))
{
cout<< "digit only"<< endl;
cin>> a;
}
;
system ("PAUSE");
return 0;
}
Why lord?? WHYYY!!??
Anyway thank you in advance for your attention and help.
Re: Whhat is wrong with this isdigit
Quote:
Originally Posted by
hayloiuy
I just wanted to see whether it is a digit or not but it gave a night of headache
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
int a;
cin>>a;
while(!isdigit(a))
{
cout<< "digit only"<< endl;
cin>> a;
}
;
system ("PAUSE");
return 0;
}
Why lord?? WHYYY!!??
Anyway thank you in advance for your attention and help.
You are probably confusing integer values, and their ASCII representations.
isdigit takes an int as an arguments, and interprets it as a char (why it does this rather than take an upfront char is complicated and not interesting).
Furthermore, you are reading your stream into an int.
So if you type "90", your int will be given the value 90, and isdigit will interpret that a a " "(space)(I believe) and return false.
If you type "h", the cin read will fail, because a is an int.
Is there a reason why you are looping, yet doing nothing in your loop? You are probably going to loop for a long time.
Re: Whhat is wrong with this isdigit
I want to create a program that ask user for a number then checks whether it is integer or not. If it is an integer it will quit. Else it will ask the user to key in the value until the user key in a digit value. How to do it???
Re: Whhat is wrong with this isdigit
Quote:
Originally Posted by
hayloiuy
I want to create a program that ask user for a number then checks whether it is integer or not. If it is an integer it will quit. Else it will ask the user to key in the value until the user key in a digit value. How to do it???
I figured, but I was answering your question about isdigit.
This is what you want.
http://www.parashift.com/c++-faq-lit....html#faq-15.3
I'd recommend you read points 1-6 though.
I'd take the time to answer, but this faq is better worded than what I can say. If you still have questions, I can answer them though.
Re: Whhat is wrong with this isdigit
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
int a;
while( cin>>a)
{
cout<< "digit only"<< endl;
cin>>a;
}
;
system ("PAUSE");
return 0;
}
I did according to the article (i hope so)
Apparently it doesn't work properly. Try compiling and testing it and you will understand.
Re: Whhat is wrong with this isdigit
Re-read section 15.3 in the link provided earlier. You did not call clear() or ignore()
as shown in that section.
Re: Whhat is wrong with this isdigit
There's no need to read two inputs per loop.