Decimal to binary and binary to decimal convertor Please review this code. How can I optimise this code?

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt; // std::reverse
#include &lt;cmath&gt; // pow()

typedef unsigned long long int ulli;

bool is_binary(ulli num)
{
    bool status = true;
    while (true)
    {
        if (num == 0)
        {
            break;
        }
        else
        {
            int temp = num % 10;
            if (temp &gt; 1)
            {
                status = false;
                break;
            }
            num = num / 10;
        }
    }
    return status;
}

std::vector&lt;ulli&gt; decimal_to_binary(int val)
{
    std::vector&lt;ulli&gt; result;
    int rem;
    if (val == 0)
    {
        result.push_back(0);
        return result;
    }
    while (val != 0)
    {
        rem = val % 2;
        result.push_back(rem);
        val = val / 2;
    }
    std::reverse(result.begin(), result.end());
    return result;
}

int binary_to_decimal(ulli val)
{
    int sum = 0;
    int rem, i = 0;
    while (val &gt; 0)
    {
        rem = val % 10;
        sum += pow(2, i) * rem;
        val = val / 10;
        i++;
    }
    return sum;
}

void display(std::vector&lt;ulli&gt;&amp; vec)
{
    for (int i = 0; i &lt; vec.size(); i++)
    {
        std::cout &lt;&lt; vec[i];
    }
    std::cout &lt;&lt; '\n';
}

int main()
{
    int option;
    std::cout &lt;&lt; "1. Decimal to Binary \n2. Binary to Decimal \n";
    std::cout &lt;&lt; "Enter your option\n";
    std::cin &gt;&gt; option;

    switch(option)
    {
        case 1:
          {
             int num;
             std::cout &lt;&lt;"\nEnter Decimal number\n";
             std::cin &gt;&gt; num;
             std::vector&lt;ulli&gt; binary = decimal_to_binary(num);
             std::cout &lt;&lt; "The binary equivalent of " &lt;&lt; num &lt;&lt;" is :\n";
             display(binary);
             break;
          }
        case 2:
          {
             ulli binary_num;
             x:
             std::cout &lt;&lt; "\nEnter Binary number\n";
             std::cin &gt;&gt; binary_num;
             bool flag = is_binary(binary_num);
             if (!flag)
             {
                std::cout &lt;&lt; "The number is not binary\n";
                goto x;
             }
             int decimal = binary_to_decimal(binary_num);
             std::cout &lt;&lt; "The decimal equivalent of " &lt;&lt; binary_num &lt;&lt; " is :\n";
             std::cout &lt;&lt; decimal &lt;&lt; '\n';
             break;
          }
        default: std::cout &lt;&lt; "Enter option 1 or 2\n";
    }
}
Validate User Input:

Always check user input is what you expect. The user may enter a valid number, or they may enter a number too long to be stored in the given type, or they might enter "fifty-three".

The code should handle these cases, rather than crashing, entering an infinite loop (option 2) or producing incorrect output (option 1). These are bugs in your program that need to be fixed. Check the answers here for ideas.

One way to do this is to loop and re-request data until the user enters an expected value. This pattern can be applied to significantly improve the overall structure of your program, e.g.:

while (true)
{
    auto conversionType = ask_conversion_type();

    if (conversionType == ConversionType::BinaryToDecimal)
    {
        // ... ask user for binary input with another loop
    }
    else if (conversionType == ConversionType::DecimalToBinary)
    {
        // ... ask user for decimal input with another loop
    }
    else
    {
        // error...
    }

    // maybe we also need an exit option?
}




Testing

Write unit tests, so you can check that the various components of the code work. In the simplest case, this can be done by adding a little function like so:

void test(bool condition)
{
    if (condition)
        std::cout &lt;&lt; "pass" &lt;&lt; std::endl;
    else
        std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl;
}


and then writing some tests at the start of main() to make sure you get the output you expect for each function:

test(decimal_to_binary(0) == std::vector&lt;ulli&gt;{ 0 });
test(decimal_to_binary(2) == std::vector&lt;ulli&gt;{ 1, 0 });
//test(decimal_to_binary(-5) == um??? );


This will also help with thinking about edge cases!



Types

If each number in the vector will only store 0 or 1, maybe it doesn't need to be a vector&lt;ulli&gt;...



Context

What is the purpose of this code? If it's an excercise doing a particular thing (i.e. you have a special definition of a "binary number" you must adhere to, or you have to write your own functions), then you need to share that with us, lest it all be "optimised" away.

For example, we could just use std::bitset:

#include &lt;bitset&gt;
...

case 1:
{
    std::cout &lt;&lt; "\nEnter Decimal number\n";
    unsigned long long num;
    std::cin &gt;&gt; num;
    // check input... then:
    std::bitset&lt;64&gt; bits(num);
    std::cout &lt;&lt; "The binary equivalent of " &lt;&lt; num &lt;&lt; " is :\n" &lt;&lt; bits &lt;&lt; "\n";
    break;
}
case 2:
{
    std::cout &lt;&lt; "\nEnter Binary number\n";
    std::bitset&lt;64&gt; bits;
    std::cin &gt;&gt; bits;
    // check input... then:
    std::cout &lt;&lt; "The decimal equivalent of " &lt;&lt; bits &lt;&lt; " is :\n" &lt;&lt; bits.to_ullong() &lt;&lt; "\n";
    break;
}


The code is certainly shorter! But maybe doesn't fulfill some other requirements?