HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Base 10 to Base N conversion

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversionbasestackoverflow

Problem

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 

using namespace std;

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

    cout > convertFrom;
    cout > convertTo;

    convertFromCopy = convertFrom;

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

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

        answer = stringDigit + answer;

        convertFrom /= convertTo;                
    }
    cout << convertFromCopy << " written in base " << convertTo << " is: " << 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).

Solution

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 > convertFrom;
    std::cout > convertTo;

    std::string answer;
    int convertFromCopy = convertFrom;

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

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

        answer = stringDigit + answer;

        convertFrom /= convertTo;                
    }

    std::cout << convertFromCopy << " written in base " << convertTo << " is: " << 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 > convertFrom;
    std::cout > convertTo;

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

    std::cout << convertFrom << " written in base " << convertTo << " is: " << 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 < 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.

Code Snippets

using namespace std;
int convertFromCopy, digit, convertTo;
    unsigned int convertFrom;
    string stringDigit, answer;
int convertTo;
    unsigned int convertFrom;

    std::cout << "Num (base 10): ";
    std::cin >> convertFrom;
    std::cout << "Base: ";
    std::cin >> convertTo;

    std::string answer;
    int convertFromCopy = convertFrom;

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

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

        answer = stringDigit + answer;

        convertFrom /= convertTo;                
    }

    std::cout << convertFromCopy << " written in base " << convertTo << " is: " << answer;
int convertTo;
    unsigned int convertFrom;

    std::cout << "Num (base 10): ";
    std::cin >> convertFrom;
    std::cout << "Base: ";
    std::cin >> convertTo;

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

    std::cout << convertFrom << " written in base " << convertTo << " is: " << answer;
std::string convertFromBaseTo(unsigned int convertFrom, int convertTo) {
    std::string answer;

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

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

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

        convertFrom /= convertTo;                
    }

    return answer;
}

Context

StackExchange Code Review Q#140250, answer score: 2

Revisions (0)

No revisions yet.