Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8 other subscribers

11 Comments

  1. dineshramitc
    September 19, 2014 @ 5:01 pm

    Reblogged this on Dinesh Ram Kali..

    Reply

  2. jay shah
    April 7, 2015 @ 4:20 pm

    How to restrict user to enter fixed number in variable???example: If i want only 3 digits in int a. than user can not enter 4th digits and throw an error and system will be stopped.

    Reply

    • akshay pai
      April 7, 2015 @ 8:47 pm

      In that case you could provide a condition restricting the values to be less that 999, and if the condition doesn’t satisfy, throw an error.

      Another method would be to use regular expression to match and see of the entered number matches the format of 3 digits, for example the regular expression could be [0-9][0-9][0-9]

      Reply

    • apmccartney
      August 1, 2015 @ 2:21 am

      When working with user input, despite the sentiment of this blog entry, you really should strive to be robust. Fortunately, the c++11 regex header makes this much less painful than it’s been in the past.

      #include
      #include
      #include
      #include

      const std::regex integerPattern_3digit(“(\+|-)?[[:d:]]{0,3}”);
      std::string staging;

      std::cin >> staging;
      if (!(std::match(staging, integerPattern_3digit)) throw std::exception();
      // conventional wisdom is throwing for user input error is a bad practice

      Reply

      • apmccartney
        August 1, 2015 @ 2:23 am

        Annoyingly, word press captures the header declarations as html.

        The headers used are
        string
        iostream
        regex
        exception

        Reply

      • akshay pai
        August 5, 2015 @ 2:01 pm

        I hadn’t known this, thanks for it. Will update my post with your suggestion soon.

        Reply

  3. Validated user input in C++ #TC1017 #Mastery 27
    May 7, 2015 @ 9:19 am

    Reply

  4. Nick
    September 28, 2015 @ 7:22 pm

    A solution to the problem regex expression free

    #include
    #include
    #include

    int main()
    {
    std::string line;
    int num;
    //split onto multiple lines for readability
    while((std::cout <> num) && !(is >> line)) //re-using `line` to test for extra stuff after the number
    {
    break; //done, we got what we wanted
    }
    std::cerr << "Invalid input, try again." << std::endl;
    }
    }

    Reply

    • akshay pai
      September 29, 2015 @ 9:53 pm

      Thank you very much, will update the post with your answer.

      Reply

  5. Brad Gibson
    January 22, 2017 @ 2:28 am

    I disagree with a few of the premises, but thank you for the article.

    There are some reasons not to use regex, but in modern C++, I think it’s hard to argue that “complexity” is one of them for validating a 3-digit number.

    @apmccartney’s implementation using regex and modern C++ is the one I would recommend to your readers. If modern C++ is not a possibility, Boost has a regex library compatible with C++98/03.

    Also, the code you highlighted (from @Nick) does not compile. See here: http://ideone.com/rT5ZgL

    My experience is that by the time you have built an airtight parser without using regex (including all the time for false positives where we think we’re done, only to find out down the road that we missed a scenario), we find we would have been much better off having bitten the bullet to learn regex.

    That being said, regex can be computationally intensive and isn’t a one fit-solution for everything. But in this case, I do think it’s the right tool for the job.

    Reply

    • akshay pai
      January 22, 2017 @ 7:26 pm

      Thank you very much for your input

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *