User Input checking! Advice needed!

This is a discussion on User Input checking! Advice needed! within the C++ Programming forums, part of the General Programming Boards category; Does anybody know how to prevent input of integer input to string variables & vice versa? If there is not ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    User Input checking! Advice needed!

    Does anybody know how to prevent input of integer input to string variables & vice versa?

    If there is not a proper way then any good tips or tricks are also very much appreciated! :^)

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Code:
    #include<iostream>
    #include<limits>
    using namespace std;
    int main()
    {
        int n;
        while(!(cin>>n))
        {
            cout<<"Only ints allowed\n";
            cin.clear();
            cin.ignore(numeric_limits<int>::max(),'\n');
        }
        cout<<n<<endl;
        return 0;
    }
    Last edited by manasij7479; 12-06-2011 at 05:41 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    You read everything as a string, then try to validate it as "looking like an int", then try to convert it to see if it really is an int.

    For example, "1234567890987654321234567890987654321" might look like an int, but it would likely overflow most of the standard sizes of int you're likely to see currently.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Quote Originally Posted by Salem View Post
    You read everything as a string, then try to validate it as "looking like an int", then try to convert it to see if it really is an int.
    Wouldn't that be a roundabout way ? (assuming that the conversion to an object will require stringstreams)
    First you take data from the input stream to a string....then for easy checking, a stringstream has to be made ...and then from it, you could input into a particular type...checking first.
    What is wrong with doing that directly from the input stream ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > What is wrong with doing that directly from the input stream ?
    Because you simultaneously have to check for whether there is any left-over crap from whatever the user typed in, and also check that the conversion itself was successful.

    By the time you've "fully engineered" ever possible outcome from using cin >> var, you're not looking at that much difference in code.

    However, what will be evident however is how much more complicated the code will be (therefore more error prone).

    I suggest you try it, just to compare.

    A really good "stress" test is
    myprog.exe < myprog.exe

    Crappy handling of errors usually shows up at some point.
    But good code should just complain (perhaps complain a lot), but would otherwise exit without crashing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking User Input
    By Dusterdoo in forum C Programming
    Replies: 9
    Last Post: 06-25-2011, 04:04 PM
  2. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 11:03 PM
  3. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 06:33 PM
  4. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 11:32 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21