Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:36:41+00:00 2026-05-25T22:36:41+00:00

If I never want an integer to go over 100, is there any simple

  • 0

If I never want an integer to go over 100, is there any simple way to make sure that the integer never exceeds 100, regardless of how much the user adds to it?

For example,

50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:36:42+00:00Added an answer on May 25, 2026 at 10:36 pm

    Here is a fairly simple and fairly complete example of a simple ADT for a generic BoundedInt.

    • It uses boost/operators to avoid writing tedious (const, non-assigning) overloads.
    • The implicit conversions make it interoperable.
    • I shunned the smart optimizations (the code therefore stayed easier to adapt to e.g. a modulo version, or a version that has a lower bound as well)
    • I also shunned the direct templated overloads to convert/operate on mixed instantiations (e.g. compare a BoundedInt to a BoundedInt) for the same reason: you can probably rely on the compiler optimizing it to the same effect anyway

    Notes:

    • you need c++0x support to allow the default value for Max to take effect (constexpr support); Not needed as long as you specify Max manually

    A very simple demonstration follows.

    #include <limits>
    #include <iostream>
    #include <boost/operators.hpp>
    
    template <
        typename Int=unsigned int, 
        Int Max=std::numeric_limits<Int>::max()>
    struct BoundedInt : boost::operators<BoundedInt<Int, Max> >
    {
        BoundedInt(const Int& value) : _value(value) {}
    
        Int get() const { return std::min(Max, _value); }
        operator Int() const { return get(); }
    
        friend std::ostream& operator<<(std::ostream& os, const BoundedInt& bi)
        { return std::cout << bi.get() << " [hidden: " << bi._value << "]"; }
    
        bool operator<(const BoundedInt& x) const   { return get()<x.get(); }
        bool operator==(const BoundedInt& x) const  { return get()==x.get(); }
        BoundedInt& operator+=(const BoundedInt& x) { _value = get() + x.get(); return *this; }
        BoundedInt& operator-=(const BoundedInt& x) { _value = get() - x.get(); return *this; }
        BoundedInt& operator*=(const BoundedInt& x) { _value = get() * x.get(); return *this; }
        BoundedInt& operator/=(const BoundedInt& x) { _value = get() / x.get(); return *this; }
        BoundedInt& operator%=(const BoundedInt& x) { _value = get() % x.get(); return *this; }
        BoundedInt& operator|=(const BoundedInt& x) { _value = get() | x.get(); return *this; }
        BoundedInt& operator&=(const BoundedInt& x) { _value = get() & x.get(); return *this; }
        BoundedInt& operator^=(const BoundedInt& x) { _value = get() ^ x.get(); return *this; }
        BoundedInt& operator++() { _value = get()+1; return *this; }
        BoundedInt& operator--() { _value = get()-1; return *this; }
      private:
        Int _value;
    };
    

    Sample usage:

    typedef BoundedInt<unsigned int, 100> max100;
    
    int main()
    {
        max100 i = 1;
    
        std::cout << (i *= 10) << std::endl;
        std::cout << (i *= 6 ) << std::endl;
        std::cout << (i *= 2 ) << std::endl;
        std::cout << (i -= 40) << std::endl;
        std::cout << (i += 1 ) << std::endl;
    }
    

    Demo output:

    10 [hidden: 10]
    60 [hidden: 60]
    100 [hidden: 120]
    60 [hidden: 60]
    61 [hidden: 61]
    

    Bonus material:

    With a fully c++11 compliant compiler, you could even define a Userdefined Literal conversion:

    typedef BoundedInt<unsigned int, 100> max100;
    
    static max100 operator ""_b(unsigned int i) 
    { 
         return max100(unsigned int i); 
    }
    

    So that you could write

    max100 x = 123_b;        // 100
    int    y = 2_b*60 - 30;  //  70
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I want to use to make sure that
Never used a cache like this before. The problem is that I want to
I have a method which never returns a null object. I want to make
purpose:i want to limit the times that a task can make syscall. so,i add
Say that I have an integer-indexed array of length 400, and I want to
Is there a way to strongly type integer ID values in C#? I've recently
I've made a simple Android music player. I want to have a TextView that
I want to put the user password check to never expire. When I create
I've never developed Flash before but I have a project where I want to
I've never really understood on how to do it. I want to where where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.