program to return english words for numbers

Pages: 12
i need a program which can convert numbers you enter into their respective words till 1 million.
for example
1208-one thousand two hunderd and eight
plz help me with this src code....i'm stuck at it
What have you written so far?
Well you first need the code that will convert the number you are on to a string representation:

1 -> "one"
2 -> two
0 -> ""
8 -> "eight"

Then you need to add descriptive "strings" to describe the position, i.e. thousands, hundreds, tens and units.

So given your example the first number is a 1 and it's position is the thousands, hence "one thousand", the next is a 2 and it's position is the hundreds, hence "two hundred", the next is a 0 so skip that, and lastly 8 in the units hence "eight".

HTH
Would be cool and fast to have a hash table with numbers to words. Please post it if you make it.
^
and that hash table would be just a vector of strings.

Can your vector hold a billion numbers? Maybe. Can it hold a trillion numbers? Nope. So it's a no for the idea of "hash table".
closed account (Dy7SLyTq)
and that hash table would be just a vector of strings.
no it wouldnt. thats the opposite of a hash table

Can your vector hold a billion numbers? Maybe. Can it hold a trillion numbers? Nope. So it's a no for the idea of "hash table".

the point of the hash table is so it doesnt have to. do you know what a hash table is? it could work really well
Do you know what a hash table with key=number and value=string is? std::unordered_map<int,std::string> how many key/value pairs can it hold? Assume you have 1TB RAM?

Let say you have
1
2
3
HashMap num2word;
num2word[1] == "one"; //true
num2word[100] == "one hundred"; //true 

So why use std::unordered_map<int,std::string> over std::vector<std::string> as HashMap?

or I misunderstood
hash table with numbers to words
?

Maybe he just wants to use it to cache the answers... In this case hash map is much much better in term of memory saving. I thought he wants to calculate all numbers to words first and save them all to a hash table...
Last edited on
closed account (Dy7SLyTq)
You could do it that way. You could also make a function to print h and another for e until it prints hello world. Just because you thought of a bad way to do it doesn't mean there isn't a good one
Hi @sam
I coded this piece, it worked with my simple test, you can modifie it to fulfill your requirement.

The idea was divide the number into 3 digits per group:
1208 -> 1,208, then read the number ,read the unit, finally piece them together.

This is a C++03 code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;


void Partition(long int n, vector<int> &vPart)
{
	while (n>=1000) {
		int t=n%1000;
		vPart.push_back(t);
		n/=1000;
	}
	vPart.push_back(n);
}

string Read0_19(int n)
{
	if (n>=0 && n<=19) {
		vector<string> reading={
			"","one","two","three","four","five",
			"six","seven","eight","nine","ten",
			"eleven","twelve","thirteen","fourteen","fifteen",
			"sixteen","seventeen","eighteen","nineteen"};
		return reading[n];
	}
	else
		return "";
}

string ReadTens(int k)
{
	if (k>=2 && k<=9) {
		vector<string> reading={
			"","","twenty","thirty","fouty",
			"fifty","sixty", "seventy","eighty","ninety"};
		return reading[k];
	}
	else
		return "";
}

string ReadThreeDigits(int n)
{
	stringstream ss;
	int digit1=n/100;
	int digit2=(n%100)/10;
	int digit3=n%10;

	if (digit1>0) ss << Read0_19(digit1) << " " << "hundred" << " ";
	if (digit2>0) {
		if (digit2==1) ss << Read0_19(digit2*10+digit3);
		else ss << ReadTens(digit2) << "-" << Read0_19(digit3);
	}
	if (digit2==0 && digit3!=0) ss << " " << Read0_19(digit3);

	return ss.str();
}

string ReadNumber(long int num)
{
	vector<int> vPart;
	vector<string> vUnit={"","thousand","million","billion"};
	stringstream ss;
	Partition(num,vPart);
	for (int i=vPart.size()-1;i>=0;--i)
		ss << ReadThreeDigits(vPart[i]) << " " << vUnit[i] << " ";
	return ss.str();
}


int main()
{
	long int N;
	cin >> N;

	cout << ReadNumber(N);
}
closed account (D80DSL3A)
A search of this site for "number to english" reveals the following as 4th result on 1st page:
http://www.cplusplus.com/forum/lounge/74394/
thanx everybody....especially yueeng...
i needed source code urgently..
keep up the good work
thanx......
i needed source code urgently..

And so, this guy has learned nothing from all this ... At least he was honest by saying he needed the source code ... Next time he will use another nickname and so on ... the story continues ..
use enumeration its simple!
closed account (Dy7SLyTq)
why would they use enumeration? its not needed here.
i tried to make this program to work untill 9999.
can anybody make it run till a billion, using same set of commands i used.
#include<iostream>//include i/o header file
using namespace std;//allows use of short routines such as cout & endl
int units(int a)//decides nu9mber at units place
{
if(a<=10)
{
if (a==0)
{
cout<<"";
}
else if(a==1)
{
cout<<"one";
}
else if(a==2)
{
cout<<"two";
}
else if(a==3)
{
cout<<"three";
}
else if (a==4)
{
cout<<"four";
}
else if(a==5)
{
cout<<"five";
}
else if (a==6)
{
cout<<"six";
}
else if (a==7)
{
cout<<"seven";
}
else if (a==8)
{
cout<<"eight";
}
else if(a==9)
{
cout<<"nine";
}
else if(a==10)
{
cout<<"ten";
}
}

}
int twenties(int a)//decides if the number belongs to the range 11-20.
{
if (a>10&&a<21)
{
if(a==11)
{
cout<<"eleven"<<endl;
}
if(a==12)
{
cout<<"twelve"<<endl;
}
if (a==13)
{
cout<<"thirteen"<<endl;
}
if(a==14)
{
cout<<"fourteen"<<endl;
}
if (a==15)
{
cout<<"fifteen"<<endl;
}
if (a==16)
{
cout<<"sixteen"<<endl;
}
if (a==17)
{
cout<<"seventeen"<<endl;
}
if (a==18)
{
cout<<"eighteen"<<endl;
}
if(a==19)
{
cout<<"nineteen"<<endl;
}
if (a==20)
{
cout<<"twenty"<<endl;
}
}
}


int thousands(int number)//breaks the number to determine its english word
{
if(number>=1000)
{
int result;
result= number/1000;
units(result);
cout<<" thousand ";
return 0;
}
}

int hundreds(int number)
{
if(number>=100)
{
int result;
result=number/100;
units(result);
cout<<" hundred ";
return 0;
}
}
int double_fig(int number)//decides number from 20-100
{
int result;
result=number/10;
if(result==2)
{
cout<<"twenty";

}
if(result==3)
{
cout<<"thirty";

}
if(result==4)
{
cout<<"forty";
}
if(result==5)
{
cout<<"fifty";
}
if(result==6)
{
cout<<"sixty";
}
if(result==7)
{
cout<<"seventy";
}
if(result==8)
{
cout<<"eighty";
}
if(result==9)
{
cout<<"ninety";
}
if(result==0)
{
cout<<"";
}
}
/*int ten_thousands(int number)
{
if(number>=10000)
{
int result;
result=number/10000;
if(result>10&&result<21)
{
twenties(result);
cout<<" thousand ";
}
else if(result>20000&&result>100000)
{
double_fig(result);
cout<<" thousand ";
}

}
}*/
int main()
{
int number;
while(number!=0)//continues the loop until input=0
{
cout<<"\n";
cout<<"enter any number till 9999"<<endl;
cin>>number;
// ten_thousands(number);
thousands(number);
number=number%1000;//breaks remaining number in form of remainder
hundreds(number);
number=number%100;
if(number>20&&number<100)
{
double_fig(number);
cout<<" ";
number=number%10;
units(number);

}
else if(number>10&&number<21)
twenties(number);
else if(number<=10)
units(number);
}
cin.ignore();
cin.get();
return 0;
}
sam87matte wrote:
can anybody make it run till a billion, using same set of commands i used.
No, because there are no "commands" in C++.

Please edit your post and wrap your code in code tags:
http://www.cplusplus.com/articles/jEywvCM9/
If you don't put in this minimum effort to getting help, we won't help you.

Also, especially do no ask for people to write the code for you. We won't, and it makes us angry at you.
Last edited on
closed account (Dy7SLyTq)
No, because there are no "commands" in C++.

yes there are. return for example

We won't, and it makes us angry at you.

maybe not angry but definitly annoyed

here is what i would do for this program. read a number into std::string. then make sure it is a valid number. after that count the number of numbers and then you know how far to go. then just start converting each number to a verbal amount and append the appropriate amount to it
DTSCode wrote:
yes there are. return for example
return is an instruction, echo is a command.
closed account (Dy7SLyTq)
its the same thing. i am instructing it to return. i am commanding it to return. and if echo is a command, then in php it is a command and if echo is a command then why isnt return?
You know very well I was referring to the windows command interpreter when I said "echo". Don't try to twist my words to make me say things I didn't.
Pages: 12