Recognizing number representation (and convert to decimal) from the base prefixes Here is what appears to be a fairly easy problem:


  Write a program that prompts the user to enter several integers in any
  combination of octal, decimal, or hexadecimal, using the 0 and
  0x base prefixes; interprets the numbers correctly; and converts
  them to decimal form. Then your program should output the values in
  properly spaced columns like this:

0x4    hexadecimal    converts to     4      decimal
0123   octal          converts to     83      decimal
65     decimal        converts to     65      decimal



Here is a first (working) draft:

const string info ("This program recognizes multiple base prefixed numbers  dec:none, hex:0x, oct:0\nand converts them into decimals.\n");
const string prompt ("Please enter numbers with random base prefixes.\n&gt;&gt;");

const char octprefix ('0');
const char hexprefixLower ('x');
const char hexprefixUpper ('X');
const int inputLoop = 2;

int main(){
try{
    cout &lt;&lt; info;

    string num;
    vector&lt;string&gt; inputNumbers;

    int numDecimals(0);
    vector &lt;int&gt; inputDecimals;

    for (int i=0; i &lt; inputLoop; i++){
        // get number with prefix 
        cout &lt;&lt; prompt;
        cin &gt;&gt; num;
        inputNumbers.push_back(num);

        // return number (# of characters) back to cin stream for reinterpretation
        for (int i=0; i &lt; num.length(); i++) cin.unget();

        // unset default flags so that prefixes are considered
        // and numbers with different bases converted directly to decimals
        cin.unsetf(ios::dec | ios::oct | ios::hex);
        cin &gt;&gt; numDecimals;
        inputDecimals.push_back(numDecimals);   
    }

    // discern base from prefix
    // checks first two characters of every vector element to determine base prefix
    for (int i=0; i &lt; inputNumbers.size(); i++){
        if (inputNumbers[i][0] != octprefix){
            cout &lt;&lt; inputNumbers[i] &lt;&lt;" decimal "&lt;&lt;" converts to "&lt;&lt; inputDecimals[i]  &lt;&lt;" decimal\n";
        }
        if (inputNumbers[1][0] == octprefix &amp;&amp; (inputNumbers[1][1] != hexprefixLower &amp;&amp; inputNumbers[1][1] != hexprefixUpper)){
                cout &lt;&lt; inputNumbers[i] &lt;&lt;" octal "&lt;&lt;" converts to " &lt;&lt; inputDecimals[i] &lt;&lt;" decimal\n";
        }
        if (inputNumbers[1][0] == octprefix &amp;&amp; (inputNumbers[i][1] == hexprefixLower || inputNumbers[i][1] == hexprefixUpper)){
                cout &lt;&lt; inputNumbers[i] &lt;&lt;" hexadecimal "&lt;&lt;" converts to " &lt;&lt; inputDecimals[i] &lt;&lt;" decimal\n";  
        }
    }

}catch(exception&amp; e){
    cerr &lt;&lt; e.what() &lt;&lt; endl;
    getchar();
}

getchar(); 
getchar();
return 0;
}


Are there any existent, unknown to me, language facilities that can be used to convert string to number that contains base prefix (0x, 0, i.e. numbers like: 020, 0x16, etc); facilities that recognize number base representation? 

I'm trying to make it look more modular and separate it in different functions with a single task (read input, prefix recognition algorithm, print result), any ideas are welcome!
I see a number of things which may help you improve your code.

Be aware of what the standard doesn't say

Your use of unget() is not portable and will not work reliably.  The standard does not guarantee that an infinite number of characters will be buffered, and that's if the stream is buffered at all.  See this question for more details.

Use standard library functions where available

There is already a function std::stoi() which does the conversion you seek.  That is, it converts a std::string into an integer, applying the base conversions as specified.

Create functions to clarify your code

One thing that the code must do is to identify which base is specified by the source string.  Since what you need is a string identifying the base, such as "hexadecimal", given an input number as a string, we can write something simple like this:

const std::string&amp; baseid(const std::string&amp; str)
{
    static const std::string bases[]{
        "decimal", "octal", "hexadecimal"
    };
    enum baseid { DEC, OCT, HEX };
    if ((str.length() &lt; 2) || (str[0] != '0'))
        return bases[DEC];
    return str[1] == 'x' ? bases[HEX] : bases[OCT];
}


Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  Know when to use it and when not to (as when writing include headers).  

Eliminate return 0 at the end of main

When a C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no reason to put return 0; explicitly at the end of main.

Don't wrap entire programs in a try clause

Rather than take the lazy way out and have a single massive block of code within the try clause, it's generally better to isolate specific things that might fail and deal with them individually and intelligently.  If you're not going to do anything useful with the output, it's probably better not to even have a try ... catch construct.

Use the appropriate #includes

This code uses a number of things that are in specific #include files but the code (as posted) does not show them.  Generally, reviewers have an easier time if you post an entire compilable program instead of just a non-compilable extract.

An alternative approach

Using the baseid function shown above, here's a much simpler program to accomplish this this:

numbase.cpp

#include &lt;iostream&gt;
#include &lt;string&gt; 
#include &lt;vector&gt;


const std::string&amp; baseid(const std::string&amp; str)
{
    static const std::string bases[]{
        "decimal", "octal", "hexadecimal"
    };
    enum baseid { DEC, OCT, HEX };
    if ((str.length() &lt; 2) || (str[0] != '0'))
        return bases[DEC];
    return str[1] == 'x' ? bases[HEX] : bases[OCT];
}

int main(){
    const std::string info ("This program recognizes multiple base prefixed numbers  dec:none, hex:0, oct:0x\nand converts them into decimals.\n");
    std::cout &lt;&lt; info;
    std::string num;
    std::vector&lt;std::string&gt; inputNumbers;

    while (std::cin &gt;&gt; num) 
        inputNumbers.push_back(num);
    for (const auto &amp;n : inputNumbers)
        std::cout &lt;&lt; n &lt;&lt; " " &lt;&lt; baseid(n) &lt;&lt;" converts to " 
                &lt;&lt; stoi(n,0,0) &lt;&lt;" decimal\n";  
}