Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Block users from typing non-digits CIN [C++]

    Is there a way, using CIN, to block users from entering non-digit characters?
    For example - I am prompting for the user to enter the Width of a box, obvious the response "abc" is not going to work ... aside from validating (getline(...) and return an error message) is there a way to simply block the user from entering it? So that hitting "a" just doesn't output anything for that CIN?

    Any help would be much appreciated.
    Thanks,

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this way:
    Code:
    int myx;
    while(cin >> myx){
      cout << "not a number";
    }
    best regards


  •  

    Posting Permissions

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