CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    26

    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.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Whhat is wrong with this isdigit

    Quote Originally Posted by hayloiuy View Post
    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    26

    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???

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Whhat is wrong with this isdigit

    Quote Originally Posted by hayloiuy View Post
    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.

  5. #5
    Join Date
    Apr 2010
    Posts
    26

    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.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured