how to define a range of type in C++ or C?

This is a discussion on how to define a range of type in C++ or C? within the C++ Programming forums, part of the General Programming Boards category; I want to define a special range of type in C++ or C...Ex: range of variable from -1000 to 1000 ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    how to define a range of type in C++ or C?

    I want to define a special range of type in C++ or C...Ex: range of variable from -1000 to 1000 in integer...So what I have to do ? please help me..thanks very much...

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What do you want to do, exactly? Do you want to have a variable that holds a range, or a variable that only stores numbers within a certain range?

    If the latter is your case, just don't set an integer to anything outside of the given range. Validate the data your user enters... a simple if(your_number_is_too_big > some_value) printf("Not. So. Big.");
    You could, I suppose, create a class, something like:
    Code:
    class MyInt
    {
       int m_value, m_range_low, m_range_high;
    public:
       MyInt(range_low, range_high)
       {
          m_range_low = range_low;
          // etc.
       }
       MyInt &operator = (const int &i)
       {
          if(i < m_range_low) m_value = m_range_low;
          else if(i > m_range_high) m_value = m_range_high;
          else m_value = i;
       }
       // implement other functions for range setting, etc.
    };
    Edit: Of course, the above merely truncates anything out of range. You could have it do other things, there is, I believe, an out of range exception within the std namespace. The user might not like their input being silently changed without their knowledge.
    Last edited by Cactus_Hugger; 03-23-2007 at 12:51 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    emmm... i think u would have to use pointers.

    Code:
    int *Point;
    *Point = new int;
    if (*Point = 0; *Point < -1000 || *Point > 1000)
    {
        delete *Point;
    }

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by beene
    Code:
    int *Point;
    *Point = new int;
    if (*Point = 0; *Point < -1000 || *Point > 1000)
    {
        delete *Point;
    }
    That won't come close to compiling. Most of that (except what's in the if() statement, I can't make out what you intended) could be done without the use of pointers.
    It seems to be the easiest option would be:
    Code:
    if(users_value > max_value || users_value < min_value)
    {
       std::cerr << "No. Enter a value for something between some-number and some-other-number." << std::endl;
       // Proceed to abort or reprompt user.
    }
    Though more info from the OP wouldn't be bad, to keep the board from having to guess or assume.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Thanks for helping guys..This is my problem...
    I have to declare a complex number that its real is in [-1000,1000], its img is in [-10000,10000]....After that I have to design a storable block to show value of complex number in memory...And I don't know how to declare its real and img in these ranges...It's only about designing, not any inputting or outputting...So how to solve this problem ?
    Last edited by zaracattle; 03-23-2007 at 01:10 PM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int *Point;
    *Point = new int;
    if (*Point = 0; *Point < -1000 || *Point > 1000)
    {
        delete *Point;
    }
    Now this is really bad. You could quickly have invalid pointers all over the place.

    Take the class approach. The constructor, for example, could specify the limits of the type.

    The most important thing is to decide what exactly you want to happen if the value goes out of the bounds.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Another interpretation something like this?
    Code:
    class range
    {
    private:
        int  r, vmin, vmax;
        int *p;
    
    public:
        // CONSTRUCTOR/DESTRUCTOR
        range( const int &min_range, const int &max_range )
        {
            vmin = min_range;
            vmax = max_range;
    
            r = abs(vmin)+abs(vmax) + 1;
    
            p = new int [r];
    
            for ( int i=0; i<r; i++ )
                p[i] = vmin + i;
        }
        ~range( void )
        {
            if ( p )
                delete []p;
        }
    
        void print( void )
        {
            if ( p )
                for ( int i=0; i<r; i++ )
                    std::cout<< p[i] << ' ';
        }
    
        int &operator [] ( const int &pos )
        {
            return p[pos];
            // You could have p[pos%r], or you could 
            // throw something here.
        }
    };
    
    
    int main( void )
    {
        range my_range( -10, 10 );
        my_range.print();
    
        std::cout<< my_range[3];
        // Also, not sure if this is really 
        // what you're looking for ....
    
    
        return 0;
    }

    That's probably very buggy, but it should compile ... I think. For one thing there's very little error checking going on there.
    Last edited by twomers; 03-23-2007 at 01:20 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    sorry guys...But I mean to design a type of data that is in these ranges and doesn't have any inputting or outputting...So I can define a variable and this variable will be in these ranges...Ex: when I declare " Range x;" with Range is a type of data, I will have x's value is in [-1000,1000]...
    Thanks for helping....Waiting for your answer....

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    Ex: when I declare " Range x;" with Range is a type of data, I will have x's value is in [-1000,1000]...
    What kind of data is Range? Basically, you want to create your own integer type with a specified range?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Could you please explain more? I for one don't know what you mean.

    >> Ex: when I declare " Range x;" with Range is a type of data, I will have x's value is in [-1000,1000]
    You can do that with my code if you say: range myinstance(-1000,1000); If you don't need the inputting or outputting methods, just strike them. Or you could take away my overloaded constructor and set it to -1000 and 1000

    This?

    Code:
    class range
    {
    private:
        int p[2001];
    
    public:
        // CONSTRUCTOR/DESTRUCTOR
        range( void )
        {
            for ( int i=0; i<2001; i++ )
                p[i] = -1000 + i;
        }
    
        void print()
        {
            
            for ( int i=0; i<2001; i++ )
                std::cout<< p[i] << ' ';//= -1000 + i;
        }
    };
    
    
    int main( void )
    {
        range r;
        r.print();
    
        return 0;
    }
    Last edited by twomers; 03-23-2007 at 01:34 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by laserlight
    What kind of data is Range? Basically, you want to create your own integer type with a specified range?
    Yes, right...type of Range is Integer....

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And what do you want to do with this range?

    I have to declare a complex number that its real is in [-1000,1000], its img is in [-10000,10000]....After that I have to design a storable block to show value of complex number in memory
    So, what's the problem? Do you want to pick random values within those ranges?

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    But why maximise your range? That doesn't make sense. Just make a functional model of your problem, then worry about the ranges.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Ahh..when anybody declare this type, if they input a real part and img part of number and their values are out of these ranges, it will automatically change to type in these ranges...
    Thanks for helping....

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    Quote Originally Posted by twomers
    Could you please explain more? I for one don't know what you mean.

    >> Ex: when I declare " Range x;" with Range is a type of data, I will have x's value is in [-1000,1000]
    You can do that with my code if you say: range myinstance(-1000,1000); If you don't need the inputting or outputting methods, just strike them. Or you could take away my overloaded constructor and set it to -1000 and 1000

    This?

    Code:
    class range
    {
    private:
        int p[2001];
    
    public:
        // CONSTRUCTOR/DESTRUCTOR
        range( void )
        {
            for ( int i=0; i<2001; i++ )
                p[i] = -1000 + i;
        }
    
        void print()
        {
            
            for ( int i=0; i<2001; i++ )
                std::cout<< p[i] << ' ';//= -1000 + i;
        }
    };
    
    
    int main( void )
    {
        range r;
        r.print();
    
        return 0;
    }
    I will try this way..Thanks very much...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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