Base 10 to Base N conversion Before I begin using this code in vital systems, I wanted to quadruple-check that it was soundproof. I plan to implement this as part of a larger program, obviously not by itself. Are there any clear flaws (output is incorrect) or optimization issues in this code?

#include &lt;iostream&gt;

using namespace std;

int main() {
    int convertFromCopy, digit, convertTo;
    unsigned int convertFrom;
    string stringDigit, answer;

    cout &lt;&lt; "Num (base 10): ";
    cin &gt;&gt; convertFrom;
    cout &lt;&lt; "Base: ";
    cin &gt;&gt; convertTo;

    convertFromCopy = convertFrom;

    while (convertFrom != 0){
        digit = convertFrom % convertTo;

        if (digit &lt; 10)
            stringDigit = '0' + digit;
        else
            stringDigit = digit - 10 + 'A';

        answer = stringDigit + answer;

        convertFrom /= convertTo;                
    }
    cout &lt;&lt; convertFromCopy &lt;&lt; " written in base " &lt;&lt; convertTo &lt;&lt; " is: " &lt;&lt; answer;
    return 0;
}


And as a note, the input will always be int, so I do not need to account for an incorrect input (i.e. if a char is entered where an int should go).
using namespace std;



You may have already seen Why is “using namespace std;” considered bad practice?

Personally, I just find it easier and more readable to write out std:: where necessary.  

Declare where used


    int convertFromCopy, digit, convertTo;
    unsigned int convertFrom;
    string stringDigit, answer;



In old C, you used to have to define every variable at the beginning of a block.  In modern C and all C++, you can declare them anywhere.  

    int convertTo;
    unsigned int convertFrom;

    std::cout &lt;&lt; "Num (base 10): ";
    std::cin &gt;&gt; convertFrom;
    std::cout &lt;&lt; "Base: ";
    std::cin &gt;&gt; convertTo;

    std::string answer;
    int convertFromCopy = convertFrom;

    while (convertFrom != 0){
        int digit = convertFrom % convertTo;

        std::string stringDigit;
        if (digit &lt; 10) {
            stringDigit = '0' + digit;
        } else {
            stringDigit = digit - 10 + 'A';
        }

        answer = stringDigit + answer;

        convertFrom /= convertTo;                
    }

    std::cout &lt;&lt; convertFromCopy &lt;&lt; " written in base " &lt;&lt; convertTo &lt;&lt; " is: " &lt;&lt; answer;


I greatly prefer to use the block form for control statements (i.e. add {} to if and else here).  I find it both easier to read and less subject to editing errors.  

Abstract into functions

    int convertTo;
    unsigned int convertFrom;

    std::cout &lt;&lt; "Num (base 10): ";
    std::cin &gt;&gt; convertFrom;
    std::cout &lt;&lt; "Base: ";
    std::cin &gt;&gt; convertTo;

    std::string answer = convertFromBaseTo(convertFrom, convertTo);

    std::cout &lt;&lt; convertFrom &lt;&lt; " written in base " &lt;&lt; convertTo &lt;&lt; " is: " &lt;&lt; answer;


Note that the function removes the need for the convertFromCopy variable.  

std::string convertFromBaseTo(unsigned int convertFrom, int convertTo) {
    std::string answer;

    while (convertFrom != 0){
        int digit = convertFrom % convertTo;

        char stringDigit;
        if (digit &lt; 10) {
            stringDigit = '0' + digit;
        } else {
            stringDigit = digit - 10 + 'A';
        }

        answer.insert(answer.begin(), stringDigit);

        convertFrom /= convertTo;                
    }

    return answer;
}


I changed stringDigit to a char because it's just a single character.  Adding to answer changed a bit as well.