#how to deal with integer overflow on arrays part 2

2 messages · Page 1 of 1 (latest)

tender flame
#

well i showed a code recently but couldn't find that topic i created. Who made just use arrays to deal with printing beyond the value of an integer, the special case is when i wanna print a big binary number out of a result of a octal to binary convertion, see the code bellow:

#include <iostream>
#include <cmath>

using namespace std;

long long convertOctalToBinary(int);
int main()
{
    int octalNumber;

    cout << "Enter an octal number: ";
    cin >> octalNumber;

    cout << octalNumber << " in octal = " << convertOctalToBinary(octalNumber) << " in binary";

    return 0;
}

long long convertOctalToBinary(int octalNumber)
{
    int decimalNumber = 0, i = 0;
    long long binaryNumber = 0;

    while(octalNumber > 0)
    {
        decimalNumber += (octalNumber%10) * pow(8,i);
        ++i;
        octalNumber/=10;
    }

    i = 1;
    cout << "decimal is: " << decimalNumber << endl;

    while (decimalNumber > 0)
    {
        binaryNumber += (decimalNumber % 2) * i;
        decimalNumber /= 2;
        i *= 10;
    }

    return binaryNumber;
}

sterile spokeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.