Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-12-2006, 07:12 PM   #1 (permalink)
 
Super Techie

Join Date: Jan 2006

Posts: 303

KryptoKnight

Default weird c++ problem...

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
__________________
Getting a new computer: specs coming soon...
KryptoKnight is offline  
Old 02-13-2006, 03:45 PM   #2 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

The integer data type can only hold up to 37,000 or around there.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 02-22-2006, 06:15 PM   #3 (permalink)
 
True Techie

Join Date: Jul 2004

Posts: 235

crazybeans

Default

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
__________________
Come visit a new technology and digital image arena website where you may compete in monthly image contests and chat about technology. t3ch.l33t is currently searching for members to help manage different areas of the site. t3ch.l33t Image Arena Home

http://img.photobucket.com/albums/v3...ch_l33tsig.jpg
crazybeans is offline  
Old 02-24-2006, 10:10 PM   #4 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

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..
__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
Old 02-25-2006, 01:07 AM   #5 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

Quote:
Originally posted by furtivefelon
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 = new char[0xff];

Cache is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On