validating an integer length in C++

ghidu

Senior member
Feb 28, 2005
331
0
0
how to validate that an integer doesn't have more than 10 digits in C++? I don't want to use arrays. thanks
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: ghidu
how to validate that an integer doesn't have more than 10 digits in C++? I don't want to use arrays. thanks

how about:
if(num <= 9999999999)
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Well, I'm making a guess here..... wouldn't this work? There's probably some obscure faster way...
 

ghidu

Senior member
Feb 28, 2005
331
0
0
Originally posted by: mugs

how about:
if(num <= 9999999999)

the thing is that if I input an integer with let's say 11 digits, the program is transforming it to a 10 digits one. here is an example:
12345678901
2147483647
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
What you are seeing is the positive bounds of a 32-bit signed integer (((2^32)/2)-1) = 2147483647. You need to use a 64-bit to store the biggest 10 digit number. Declare it like so (i being the variable). In your current code, it may be overflowing the buffer (bad!)

Either signed or unsigned 64-bit will store the biggest 10 digit number. Unsigned 32-bit still only stores from 0 to 4294967295.

Signed 64-bit: -9223372036854775807 to 9223372036854775806
Unsigned 64-bit: 0 to 18446744073709551615

signed __int64 i;
//or this will work too I think. int64 makes sure it's 64-bit though because it could vary per compiler or architecture.
signed long i;

How are you going to deal with negatives? Does the minus count as a digit?
 

ghidu

Senior member
Feb 28, 2005
331
0
0
Originally posted by: xtknight
What you are seeing is the positive bounds of a 32-bit signed integer (((2^32)/2)-1) = 2147483647. You need to use a 64-bit to store the biggest 10 digit number. Declare it like so (i being the variable). In your current code, it may be overflowing the buffer (bad!)

Either signed or unsigned 64-bit will store the biggest 10 digit number. Unsigned 32-bit still only stores from 0 to 4294967295.

Signed 64-bit: -9223372036854775807 to 9223372036854775806
Unsigned 64-bit: 0 to 18446744073709551615

signed __int64 i;
//or this will work too I think. int64 makes sure it's 64-bit though because it could vary per compiler or architecture.
signed long i;

How are you going to deal with negatives? Does the minus count as a digit?

xtknight, I need to check a positive integer inputed. If there are more than 10 digits print an error, if not continue with the program. I don't know if I'm allowed to use the 64 architecture.I thought of using cin.get and count
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
OK...this should work.

__int64 i;
//now get the number in i

if (i<=9999999999) {
//operation
}

Doesn't matter if you use signed or unsigned for a 10-digit positive number. The 64-bit integer has nothing to do with a 64-bit CPU. It should work on anything as far as I know. It's only the default size of an integer that can potentially vary. __int64 forces a 64-bit integer.

Actually instead you may want to do it by strings in case the number overflows the integer.

char* number = std::cin.getline();
//check with strlen

Something like that?? You should probably use string with C++ though. Sorry, not sure how to use the string class. This will probably work. Somebody correct me here.
 

ghidu

Senior member
Feb 28, 2005
331
0
0
Originally posted by: xtknight
OK...this should work.

__int64 i;
//now get the number in i

if (i<=9999999999) {
//operation
}

Doesn't matter if you use signed or unsigned for a 10-digit positive number. The 64-bit integer has nothing to do with a 64-bit CPU. It should work on anything as far as I know. It's only the default size of an integer that can potentially vary. __int64 forces a 64-bit integer.

Actually instead you may want to do it by strings in case the number overflows the integer.

char* number = std::cin.getline();
//check with strlen

Something like that?? You should probably use string with C++ though. Not sure how to use the string type. This will probably work. Somebody correct me here.

as I said earlier, this is what I thought of doing using cin.get():
d=cin.get();
while(d!='\n'||count<=10)
{
num=num*10+d;
count++;
d=cin.get;
}

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: ghidu
d=cin.get();
while(d!='\n'||count<=10)
{
num=num*10+d;
count++;
d=cin.get;
}

OK...does it work? You should just use getline then convert it to an integer besides going through the loop me thinks (though the way you do it is quite clever). Either way it should prevent a buffer overflow unless you explicitly set it the string to a maximum length.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Wait, there is a problem with your code...

What if count is greater than 10? Say count is 15. It'll only process the first 10 digits unless I'm out of my mind (possible :)) ? It has no way of knowing whether it was 10 digits or whether it was more than 10 digits. Try the getline() and length() way.
 

ghidu

Senior member
Feb 28, 2005
331
0
0
Originally posted by: xtknight
Wait, there is a problem with your code...

What if count is greater than 10? Say count is 15. It'll only process the first 10 digits unless I'm out of my mind (possible :)) ? It has no way of knowing whether it was 10 digits or whether it was more than 10 digits. Try the getline() and length() way.

if count is greater than 10 it's going to print an error :"The number has more than 10 digits"
the while is only to count the digits and build the number. it will stop the moment count=11. then an if and I'm done. I have to deal with the negative input , like the first d='-', or something like that.
 

itachi

Senior member
Aug 17, 2004
390
0
0
64-bit numbers have been used way before 64-bit architectures came out.

static const long MAX_INT = 0x0ffffffff;
long num;
cin >> num;

bool overflow = (num & (~MAX_INT)) != 0;
int signed = (int)(num >>> 63);
if(overflow && !signed) throw exception();

char buf[12];
itoa((int)(num & MAX_INT), buf, 10);
 

ghidu

Senior member
Feb 28, 2005
331
0
0
I'm not allowed to use string nor arrays. xtknight I tried your idea with 64 bit, it doesn't work