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

Convert number to words

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

Problem

I wrote this converter code. It can convert a number from 1-100 into words. It works perfectly, but it seems to be too complicated to me.

Can you tell me whether it's okay or not?

#include 

int main()
{
    int number;
    int first_digit;
    int second_digit;
    std::cout > number;
    first_digit = number/10;
    second_digit = number%10;
    if(number >= 11 && number  1 && number != 100) std::cout<<"-";
        switch(second_digit)
        {
            case 1:
                std::cout<<"one";
                break;
            case 2:
                std::cout<<"two";
                break;
            case 3:
                std::cout<<"three";
                break;
            case 4:
                std::cout<<"four";
                break;
            case 5:
                std::cout<<"five";
                break;
            case 6:
                std::cout<<"six";
                break;
            case 7:
                std::cout<<"seven";
                break;
            case 8:
                std::cout<<"eight";
                break;
            case 9:
                std::cout<<"nine";
                break;
            default:
                break;

        }
    }
    return 0;
}

Solution

I've never tackled such a problem before, so I'll proceed with this cautiously.

-
You don't need to declare those three variables and then assign to them. Instead, initialize them right away where they're first used.

You can also move number right above the cin for slightly closer scope.

std::cout > number;
int first_digit = number/10;
int second_digit = number%10;


-
I suppose switch is an okay choice for this program. Either way, the switch can still be condensed while making the program more modular (with functions).

Here's what 11-20 could look like within its own function:

std::string elevenThroughTwenty(unsigned int number)
{
    switch (number)
    {
        case 11: return "eleven";
        // ...
        case 20: return "twenty"; // you forgot the 20 in your code

        // throw an exception if number not in the switch
        // include  to use this
        default: throw std::logic_error("Not 11 through 20");
    }
}


Separating them as such helps with readability, organization, and conciseness. You will also need proper defaults so that invalid numbers don't cause crippling errors.

Code Snippets

std::cout << "Enter the number: ";
int number;
std::cin >> number;
int first_digit = number/10;
int second_digit = number%10;
std::string elevenThroughTwenty(unsigned int number)
{
    switch (number)
    {
        case 11: return "eleven";
        // ...
        case 20: return "twenty"; // you forgot the 20 in your code

        // throw an exception if number not in the switch
        // include <stdexcept> to use this
        default: throw std::logic_error("Not 11 through 20");
    }
}

Context

StackExchange Code Review Q#30525, answer score: 5

Revisions (0)

No revisions yet.