snippetcppMinor
Convert number into words
Viewed 0 times
convertintowordsnumber
Problem
I am trying to implement a program which can convert number into words. My code can convert numbers between 0 - 999. It uses recursive function calls and simple arithmetic operations. Can you please review it and give me your feedback?
I used three different vectors to store words such as "Six" and "Eleven" and then access there indexes according to your input.
numberToWord.h
Source.cpp
```
#include
#include "numberToWord.h"
numbertoword::numbertoword() : xN(0), yN(0), originalNumber(0)
{
}
void numbertoword::initialize()
{
//one to nine
one2nine.push_back("Zero");
one2nine.push_back("One");
one2nine.push_back("Two");
one2nine.push_back("Three");
one2nine.push_back("Four");
one2nine.push_back("Five");
one2nine.push_back("Six");
one2nine.push_back("Seven");
one2nine.push_back("Eight");
one2nine.push_back("Nine");
//eleven to ninteen
elevent2ninteen.push_back("Eleven");
elevent2ninteen.push_back("Twelve");
elevent2ninteen.push_back("Thirteen");
elevent2ninteen.push_back("Fourteen");
elevent2ninteen.push_back("Fifteen");
elevent2ninteen.push_back("Sixteen");
elevent2ninteen.push_back("Seventeen");
elevent2ninteen.push_back("Eighteen");
elevent2ninteen.push_back("Nineteen");
//TwoDigit
twoDigit.push_back("Ten");
twoDigit.push_back("Twenty");
twoDigit.push_back("Thirty");
twoDigit.push_back("Forty");
twoDigit.push_back("Fifty");
twoDigit.push_back("Sixty");
I used three different vectors to store words such as "Six" and "Eleven" and then access there indexes according to your input.
numberToWord.h
#ifndef numberToWord_H
#define numberToWord_H
#include
#include
class numbertoword
{
public:
numbertoword();
~numbertoword(){};
public:
std::vector one2nine;
std::vector elevent2ninteen;
std::vector twoDigit;
std::vector threeDigit;
public:
void initialize();
void convert(int number);
int countNumber(int number);
void calculate(int number, int count);
void display(int digit);
private:
int xN, yN;
int originalNumber;
};
#endifSource.cpp
```
#include
#include "numberToWord.h"
numbertoword::numbertoword() : xN(0), yN(0), originalNumber(0)
{
}
void numbertoword::initialize()
{
//one to nine
one2nine.push_back("Zero");
one2nine.push_back("One");
one2nine.push_back("Two");
one2nine.push_back("Three");
one2nine.push_back("Four");
one2nine.push_back("Five");
one2nine.push_back("Six");
one2nine.push_back("Seven");
one2nine.push_back("Eight");
one2nine.push_back("Nine");
//eleven to ninteen
elevent2ninteen.push_back("Eleven");
elevent2ninteen.push_back("Twelve");
elevent2ninteen.push_back("Thirteen");
elevent2ninteen.push_back("Fourteen");
elevent2ninteen.push_back("Fifteen");
elevent2ninteen.push_back("Sixteen");
elevent2ninteen.push_back("Seventeen");
elevent2ninteen.push_back("Eighteen");
elevent2ninteen.push_back("Nineteen");
//TwoDigit
twoDigit.push_back("Ten");
twoDigit.push_back("Twenty");
twoDigit.push_back("Thirty");
twoDigit.push_back("Forty");
twoDigit.push_back("Fifty");
twoDigit.push_back("Sixty");
Solution
numberToWord.h
-
Consider renaming
-
Since you're not using your own destructor, you can just leave it out. The compiler will provide one for you that should be suitable.
-
These names make no sense to me:
Unless these shortened names are obvious in the program, they should be spelled-out entirely so that others can understand them. It may also benefit you in case you ever forget what they mean.
Source.cpp
-
Elaborating on what @vnp has mentioned about initializer lists, you can use it in place of the multiple calls to
Moreover, consider renaming the vector to
Better yet (regarding the use of vectors), you can use
-
This can be simplified:
by using the
-
With the existing curly braces, these should be on separate lines:
-
You should be doing input-validation with this. If the user inputs a non-numerical value or a value below 0 or above 999, the program should display an appropriate error message and then terminate.
-
This program can be made more usable by accepting command line arguments:
(
In order for this to work, you should replace the default initializer list with one that takes an argument:
(I've rearranged the list so that it's easier to maintain the data members.)
-
Consider renaming
numbertoword to NumberToWord. This uses a naming convention referred to as PascalCase, which is different from your variables and functions. Also notice that the compound words have been emphasized. This makes it easier for others to read the full name.-
Since you're not using your own destructor, you can just leave it out. The compiler will provide one for you that should be suitable.
-
These names make no sense to me:
int xN, yN;Unless these shortened names are obvious in the program, they should be spelled-out entirely so that others can understand them. It may also benefit you in case you ever forget what they mean.
Source.cpp
-
Elaborating on what @vnp has mentioned about initializer lists, you can use it in place of the multiple calls to
push_back():one2nine { "one", "two", "three" /* ... */ };Moreover, consider renaming the vector to
oneToNine, which is less-awkward of a name. Apply this to the other similar names as well.Better yet (regarding the use of vectors), you can use
std::array instead. It would be better-suited for this task as you're not needing a dynamic data structure here.-
This can be simplified:
number = number / 10;by using the
/= operator:number /= 10;-
With the existing curly braces, these should be on separate lines:
display(number); return;-
You should be doing input-validation with this. If the user inputs a non-numerical value or a value below 0 or above 999, the program should display an appropriate error message and then terminate.
-
This program can be made more usable by accepting command line arguments:
int main(int argc, char* argv[])
{
int number;
// the file name is considered an argument
if (argc > 1)
{
number = std::atoi(argv[1]);
}
// only file name given
else if (argc == 1)
{
std::cin >> number;
}
// ...
}(
std::atoi() requires `)
-
It would make more sense to construct numbertoword objects with the original number, rather than passing it to convert()`. The function has no business doing that, and this initialization should only be done by the constructor. The function should also no longer take arguments.numbertoword n2w(999);
n2w.convert();In order for this to work, you should replace the default initializer list with one that takes an argument:
numbertoword::numbertoword(int originalNumber)
: xN(0)
, yN(0)
, originalNumber(originalNumber)
{}(I've rearranged the list so that it's easier to maintain the data members.)
Code Snippets
int xN, yN;one2nine { "one", "two", "three" /* ... */ };number = number / 10;number /= 10;display(number); return;Context
StackExchange Code Review Q#63518, answer score: 8
Revisions (0)
No revisions yet.