Only number can be accepted
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Only number can be accepted

  1. #1
    Join Date
    Jan 2012
    Posts
    54

    Only number can be accepted

    Helo

    i am preparing project,in which i want to perform operation like user can only enter number 0 to 9 and can't enter alphabet or special symbol...for i have try many code but can't perform.... any one help me?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Only number can be accepted

    What type of application are you preparing: console or UI. for what system (Windows, Unix, ...)?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Only number can be accepted

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Jan 2012
    Posts
    54

    Re: Only number can be accepted

    I am using windows 7 n i have switch case in which i want to do this only accept digit if user enter alphabet then message will be print

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Only number can be accepted

    Then check every character to be between 0 and 9, if not - don't accept this character.
    If your app is a GUI one then the best way is to handle WM_CHAR message
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2012
    Posts
    54

    Re: Only number can be accepted

    i m not working in GUI. isdigit() do not work.
    int ch;
    do
    {
    cout<<enter choice;
    cout<<1
    cout<<2
    cout<<3
    cin>> ch
    int c=isdigit(ch)
    if(c!=0)
    {
    switch(ch)
    {
    case 1:cout<<"one";break
    case 2:cout<<"two";break
    case 3:exit;
    }
    else
    cout<<"Enter only digit";
    }
    }while(!(ch==3));

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Only number can be accepted

    Is your build UNICODE or MBCS?
    If UNICODE - try using iswdigit instead.
    Victor Nijegorodov

  8. #8
    Join Date
    Jan 2012
    Posts
    54

    Re: Only number can be accepted

    i have tries iswdigit() and also include <ctype.h> and <wchar.h> but error occur at compilation time "Unable to open file wchar.h

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Only number can be accepted

    Quote Originally Posted by CSharpque View Post
    i have tries iswdigit() and also include <ctype.h> and <wchar.h> but error occur at compilation time "Unable to open file wchar.h
    Then don't include them!
    And, BTW, you didn't answer yet:
    UNICODE or MBCS?
    Victor Nijegorodov

  10. #10
    Join Date
    Jan 2012
    Posts
    54

    Re: Only number can be accepted

    UNICode n if don't include it then error occur "iswdigit should have prototype"

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Only number can be accepted

    What compiler do you use?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Only number can be accepted

    Quote Originally Posted by CSharpque View Post
    i have tries iswdigit() and also include <ctype.h> and <wchar.h> but error occur at compilation time "Unable to open file wchar.h
    Could you show your actual code (please, use Code tags!) with
    Code:
    #include <ctype.h>
    and exact error message?
    Victor Nijegorodov

  13. #13
    Join Date
    Jan 2012
    Posts
    54

    Re: Only number can be accepted

    if i include <ctype.h> it will give error "Function iswdigit() should have prototype" so i delete <ctype.h> and include <wchar.h> file but it also gives 2 error "Unable to open include file 'WCHAR.H'" and second error "Function iswdigit() should have prototype"
    Last edited by CSharpque; February 23rd, 2012 at 11:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center