# Numbers to words

This is a discussion on Numbers to words within the C++ Programming forums, part of the General Programming Boards category; So i'm currently reading the website host book, and one of the situations we are addressed with is converting numbers ...

1. ## Numbers to words

So i'm currently reading the website host book, and one of the situations we are addressed with is converting numbers into their text, e.g. 103; one hundred three. Well i was able to finally get all my code to work. But i'm wondering if i didn't go about this very efficiently can somebody look over my code and tell me? Also I'm not using pointers yet i think that might have made this easier but not too sure.

Code:
```#include <iostream>

using namespace std;

int tens(int remainder);
int hundreds(int x);
int thousands(int x);
void numtoword(int x);
void hardcode(int x);

int main()
{
int x;
cout<<"What number do you want to convert to text?\n";
cin>>x;
numtoword(x);

return 0;
}

void numtoword(int x)
{
if (x < 20){
hardcode(x);
}
else if (x > 99 && x < 1000){
hundreds(x);
}
else if(x > 999 && x < 1000000){
thousands(x);
}

}

void hardcode(int x)
{
if(x == 1){
cout<<" One ";
}
else if(x == 2){
cout<<" Two ";
}
else if(x == 3){
cout<<" Three ";
}
else if(x == 4){
cout<<" Four ";
}
else if(x == 5){
cout<<" Five ";
}
else if(x == 6){
cout<<" Six ";
}
else if(x == 7){
cout<<" Seven ";
}
else if(x == 8){
cout<<" Eight ";
}
else if(x == 9){
cout<<" Nine ";
}
else if(x == 10){
cout<<" Ten";
}
else if(x == 11){
cout<<" Eleven";
}
else if(x == 12){
cout<<" Tweleve";
}
else if(x == 13){
cout<<" Thirteen";
}
else if(x == 14){
cout<<" Fourteen";
}
else if(x == 15){
cout<<" Fifteen";
}
else if(x == 16){
cout<<" Sixteen";
}
else if(x == 17){
cout<<" Seventeen";
}
else if(x == 18){
cout<<" Eighteen";
}
else if(x == 19){
cout<<" Nineteen";
}
}

int thousands(int x)
{
int firstnum = x/1000;
int bignum = x % 1000;
while(x > 1000){
x = x - 1000;
}
if (bignum < 100 && firstnum < 20){
hardcode(firstnum);
cout<<" Thousand ";
tens(bignum);

}
else if(firstnum < 20  && bignum > 99)
{
hardcode(firstnum);
cout<<" Thousand ";
hundreds(bignum);
}
else if(firstnum > 19 && firstnum < 100)
{
tens(firstnum);
cout<<" Thousand ";
hundreds(bignum);
}
else if(firstnum > 99)
{
hundreds(firstnum);
cout<< " Thousand ";
hundreds(x);

}

}

int hundreds(int x)
{
if(x > 99){
if(x < 200){
int number = x / 100;
int remainder = (x - 100);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 300){
int number = x / 100;
int remainder = (x - 200);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 400){
int number = x / 100;
int remainder = (x - 300);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 500){
int number = x / 100;
int remainder = (x - 400);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 600){
int number = x / 100;
int remainder = (x - 500);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 700){
int number = x / 100;
int remainder = (x - 600);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 800 || x == 800){
int number = x / 100;
int remainder = (x - 700);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 900){
int number = x / 100;
int remainder = (x - 800);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
else if(x < 1000){
int number = x / 100;
int remainder = (x - 900);
hardcode(number);
cout<<" Hundred ";
tens(remainder);
}
}
return 0;
}

int tens(int remainder)
{
if(remainder > 19 && remainder < 30){
cout<<" Twenty";
int digi = remainder - 20;
hardcode(digi);
}
else if(remainder > 29 && remainder < 40){
cout<<" Thirty";
int digi = remainder - 30;
hardcode(digi);
}
else if(remainder > 39 && remainder < 50){
cout<< " Fourty";
int digi = remainder - 40;
hardcode(digi);
}
else if(remainder > 49 && remainder < 60){
cout<< " Fifty";
int digi = remainder - 50;
hardcode(digi);
}
else if(remainder > 59 && remainder< 70){
cout<<" Sixty";
int digi = remainder - 60;
hardcode(digi);
}
else if(remainder > 69 && remainder < 80){
cout<<" Seventy";
int digi = remainder - 70;
hardcode(digi);
}
else if(remainder > 79 && remainder < 90){
cout<<" Eighty";
int digi = remainder - 80;
hardcode(digi);
}
else if(remainder > 89 && remainder < 100){
cout<<" Ninety";
int digi = remainder - 90;
hardcode(digi);
}
else{
hardcode(remainder);
}
return 0;
}```

2. > int number = x / 100;
> int remainder = (x - 100);
Look up what the % operator does.

And also consider something like this
Code:
```char *strings[] = { "", "one", "two", "three", "four" };
cout << strings[num];```

3. Originally Posted by Salem
And also consider something like this
Code:
```char *strings[] = { "", "one", "two", "three", "four" };
cout << strings[num];```
Hool learn how to use the C++ string class now. For sure you will need it in the past
(Of course what Salem says it's correct )

4. Salem's suggestion is good, but I would make it a static const char *[].

5. Apart from the suggestions above, the word "twelve" only contains two letter 'e's.