### counting the digits of nums

Hi.
I'm not a native, sorry if something is wrong with the writings.
I have two problems with this program.
the first one is with countOfDigit function. when I enter a number that has more than 12 digits, it return 12 as the number of digits. when I tracked the program I understood that after the 12th division, it outputs "inf". how should I stop this?
the second problem is with digitsOfNum function. It should return the number in the specified range. for example if you enter 123456, 2, 3 it should return 345 but it returns 543!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include #include #include #include #include using namespace std; int countOfDigit(double); int digitsOfNum(int, int, int); int main() { int numb, start, lentgh; cout << "Please enter the number, the starting point and the length"; cin >> numb >> start >> lentgh; cout << digitsOfNum(numb, start, lentgh); } int countOfDigit(double num) { int digits = 0; for(int i = 10; i != 0 ; i *= 10) { digits++; if ((num / i) < 1) { i = 0; } } return digits; } int digitsOfNum(int num, int start, int lentgh) { int cot = countOfDigit(num); vector digit(cot); vector digi(cot); digit.resize(cot); for(int i = 0; i < cot; i++) { int a = num % 10; digit[i] = a; num = num / 10; } for(int i = cot - 1; i >= 0; i--) { digi[i] = digit[(cot - 1) - i]; } int finalNum = 0; for(int i = 0; i < lentgh; i++) { finalNum += digi[start + i] * pow(10, i); } return finalNum; }``````
 the first one is with countOfDigit function. when I enter a number that has more than 12 digits, it return 12 as the number of digits. when I tracked the program I understood that after the 12th division, it outputs "inf". how should I stop this?

The only way is to use a variable type that can handle more than 12 digits.
Some examples are:
 ``123456789`` ``````long long int unsigned long long int std::string char* //From : http://www.cplusplus.com/reference/ratio/ratio/?kw=ratio std::tera std::peta std::exa //etc. ``````

Or as some programmers here may suggest, writing your own BigInt class. But unless you need 1000 digit numbers or something, just use the available types.

 the second problem is with digitsOfNum function. It should return the number in the specified range. for example if you enter 123456, 2, 3 it should return 345 but it returns 543!

Try reversing the order you store the numbers in the vector (or reverse the order you combine the digits).
thanks
Topic archived. No new replies allowed.