|
-
April 27th, 2010, 04:44 AM
#1
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.
-
April 27th, 2010, 04:53 AM
#2
Re: Whhat is wrong with this isdigit
 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.
-
April 27th, 2010, 06:02 AM
#3
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???
-
April 27th, 2010, 06:09 AM
#4
Re: Whhat is wrong with this isdigit
 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.
-
April 27th, 2010, 07:15 AM
#5
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.
-
April 27th, 2010, 07:24 AM
#6
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.
-
April 27th, 2010, 08:09 AM
#7
Re: Whhat is wrong with this isdigit
There's no need to read two inputs per loop.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|