weird c++ problem...

Status
Not open for further replies.

KryptoKnight

In Runtime
Messages
303
Ok.. I was making an age program, but came upon a problem... not too lifethreatening, but kind of weird. Here it is....

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int age;

cout<<"Please input your age: ";
cin>> age;
cin.ignore();
if ( age < 100 ) {
cout<<"You are pretty young... Let's all point and laugh!\n";
}
else if ( age == 100 ) {
cout<<"You are old... let's all point and laugh!\n";
}
else {
cout<<"You are really old, Sensei!\n";
}
cin.get();
}

I can enter the usual (99, 100 , 10, 1000,) but if I go any higher, like a trillion, it doesn't work and just shuts off on pressing enter
 
you may need to declare your variables differently. Here is some information on variable memory range values you can use for future problems like this one.

Note: on the left is the variable type, on the right is the maximum number alocated in memory for the variable type. I'll add the real number as well as the 2^n.

long long int = plus/minus 2^63 or 9223372036854775808
long int = plus/minus 2^31 or 2147483648
int = same as long int
short int = plus/minus 2^15 or 32768

if you ever try to add a number larger than the given range to a variable you will either "wrap" the number (go back to negative side and work your way up) or you will get a "floating point exception". Hope that helps. That information will cost you $1.00 and a Rodeo Cheeseburger :p
 
not sure about c++ (i'm pretty sure they have some kind of arbitrary precision type for a variable?), but in c, if you need a very large of anything, you use malloc..
 
furtivefelon said:
not sure about c++ (i'm pretty sure they have some kind of arbitrary precision type for a variable?), but in c, if you need a very large of anything, you use malloc..

The C++ equivalent of malloc is new.

Typical usage (much nicer than malloc ;) ):
Code:
char* pszText = [B]new[/B] char[0xff];
 
Status
Not open for further replies.
Back
Top Bottom