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

Floating-point binary/decimal/octal/hex converter

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

Problem

I've finally decided to design a class for this program, but some of the code still looks messy. I'm also a bit concerned about DRY, particularly in binaryToOctalHex(). The program is looking better, but I'm still not liking what I'm doing in the aforementioned function. I don't mind refactoring that if necessary.

What should be done here?

Driver

#include 
#include "NumberSystemsConverter.h"

int main()
{
    unsigned choice;
    std::string str;

    std::cout  Decimal/Octal/Hex\n";
    std::cout  Binary/Octal/Hex\n";
    std::cout  Binary/Decimal/Hex\n";
    std::cout  Binary/Decimal/Octal\n\n";

    do
    {
        std::cout  Conversion #: ";
        std::cin >> choice;
    } while (choice  4);

    std::cout  Value: ";
    std::cin.ignore();

    if (choice == 1)
    {
        std::string binary;
        getline(std::cin, binary);
        NumberSystemsConverter conversion(binary);
        conversion.display();
    }
    else if (choice == 2)
    {
        d32 decimal;
        std::cin >> decimal;
        NumberSystemsConverter conversion(decimal);
        conversion.display();
    }
    else if (choice == 3)
    {
        std::string octal;
        getline(std::cin, octal);
        NumberSystemsConverter conversion(8, octal);
        conversion.display();
    }
    else if (choice == 4)
    {
        std::string hex;
        getline(std::cin, hex);
        NumberSystemsConverter conversion(16, hex);
        conversion.display();
    }
}


NumberSystemsConverter.h

```
#ifndef NUMBERSYSTEMSCONVERTER_H
#define NUMBERSYSTEMSCONVERTER_H

#include
#include

typedef long double d32;
typedef std::uint32_t u32;

class NumberSystemsConverter
{
private:
d32 decimal;
std::string binary;
std::string octal;
std::string hex;

int findDecimalPoint(const std::string&) const;
void decimalToBinary();
void stringToDecimal(unsigned, const std::string&);
void binaryToOctal();
void binaryToHex();

public:
NumberSystemsConverte

Solution

Behind all the ifs in main(), I see lurking a parameter:

unsigned inputBases[] = { 2, 10, 8, 16 };
unsigned inputBase = inputBases[choice - 1];

std::string input;
std::getline(std::cin, input);
NumberSystemsConverter conversion(inputBase, input);
conversion.display();


Of course, in order for that to work, you need to make sure that all bases are treated equally in the two-args constructor. A string containing a number in base-10 or base-2 should be usable in NumberSystemsConverter(base, input). Which is just good business anyway; who wants a converter that only selectively knows how to convert? :)

That probably means making sure stringToDecimal(base, str) can handle decimal and binary as well. At which point, your constructor turns into

NumberSystemsConverter::NumberSystemsConverter(unsigned base, const std::string &str)
{
    strToDecimal(base, str);
    decimalToBinary();
    binaryToHex();
    binaryToOctal();
}


By the way, I'm not really liking that those conversion functions set results in the object rather than returning a value.

Code Snippets

unsigned inputBases[] = { 2, 10, 8, 16 };
unsigned inputBase = inputBases[choice - 1];

std::string input;
std::getline(std::cin, input);
NumberSystemsConverter conversion(inputBase, input);
conversion.display();
NumberSystemsConverter::NumberSystemsConverter(unsigned base, const std::string &str)
{
    strToDecimal(base, str);
    decimalToBinary();
    binaryToHex();
    binaryToOctal();
}

Context

StackExchange Code Review Q#25824, answer score: 4

Revisions (0)

No revisions yet.