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...
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...
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:
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.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. };
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)
emmm... i think u would have to use pointers.
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.Originally Posted by beene
It seems to be the easiest option would be:
Though more info from the OP wouldn't be bad, to keep the board from having to guess or assume.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. }
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)
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.
Now this is really bad. You could quickly have invalid pointers all over the place.Code:int *Point; *Point = new int; if (*Point = 0; *Point < -1000 || *Point > 1000) { delete *Point; }
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.
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.
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....
What kind of data is Range? Basically, you want to create your own integer type with a specified range?Ex: when I declare " Range x;" with Range is a type of data, I will have x's value is in [-1000,1000]...
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Yes, right...type of Range is Integer....Originally Posted by laserlight
And what do you want to do with this range?
So, what's the problem? Do you want to pick random values within those ranges?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
But why maximise your range? That doesn't make sense. Just make a functional model of your problem, then worry about the ranges.
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....
I will try this way..Thanks very much...Originally Posted by twomers