Number to Word

I was reading my C++ book and wanted to practice what i was reading. I encountered a programming challenge which was to make a program that converts numbers from
0-9999 to words.

ex. 4578-> four thousand five hundred and seventy eight

i already did my code (as you can see) but i only put up this topic for people to discuss more efficient ways or simply different ways of doing this program.

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
82
83
  #include<iostream>
#include<string>
using namespace::std;


class Numbers {

private:
	int num;

public:
	static string twoDigit[];
	 Numbers()
	{ num=0;}
	void setNumber(int number)
	{ num=number;}
	int getNumber()
	{ return num;}

	void print()
	{
		if(num>=1000)
		{
			int thousand;
			thousand=(num%10000-num%1000)/1000;
			cout<<twoDigit[thousand]<<" thousand ";
		}//end if

		if(num>=100)
		{
			int hundred;
			hundred=(num%1000-num%100)/100;
			cout<<twoDigit[hundred]<<" hundred and ";
		}//end if
		if(num>=0)
		{
			int ten;
			ten=num%100;
			cout<<twoDigit[ten]<<endl;
			//cout<<ten<<endl;
		}//end if

	}//end void

};

string Numbers::twoDigit[100]={"zero","one","two","three","four","five","six","seven","eight","nine",
				"ten","eleven","twelve", "thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen",
				"twenty","twenty-one","twenty-two","twenty-three","twenty-four","twenty-five","twenty-six",
				"twenty-seven","twenty-eight","twenty-nine","thirty","thirty-one","thirty-two","thirty-three",
				"thirty-four","thirty-five","thirty-six","thirty-seven","thirty-eight","thirty-nine",
				"forty","forty-one","forty-two","forty-three","forty-four","forty-five","forty-six","forty-seven",
				"forty-eigth","forty-nine","fifty","fifty-one","fifty-two","fifty-three","fifty-four","fifty-five",
				"fifty-six","fifty-seven","fifty-eight","fifty-nine","sixty", "sixty-one","sixty-two","sixty-three",
				"sixty-four","sixty-five","sixty-six","sixty-seven","sixty-eight","sixty-nine","seventy","seventy-one",
				"seventy-two","seventy-three","seventy-four","seventy-five","seventy-six","seventy-seven",
				"seventy-eight","seventy-nine","eighty","eighty-one","eighty-two","eighty-three","eighty-four",
				"eighty-five","eighty-six","eighty-seven","eighty-eight","eighty-nine","ninety","ninety-one",
				"ninety-two","ninety-three","ninety-four","ninety-five","ninety-six","ninety-seven",
				"ninety-eight","ninety-nine"};

int main ()
{

	int number;
	Numbers conversion;

	cout<<"Enter a number between the range of 0-9999 and the program"<<endl<<"will convert it to words."<<endl;
	cin>>number;

	if(number<0||number>9999)
	{
		cout<<"Error.Number is out of range."<<endl;
		cout<<"Enter a number between the range of 0-9999 and the program"<<endl<<"will convert it to words."<<endl;
		cin>>number;
	}
	conversion.setNumber(number);
	conversion.print();

	system("pause");
	return 0;

}
but i only put up this topic for people to discuss more efficient ways or simply different ways of doing this program


switch out the if condition in main for a while loop. If you enter an incorrect number twice, the program ends. get rid of system pause
thanks for noticing...i originally thought of doing it a while..guess i had something else on my mind haha
closed account (S6k9GNh0)
You can actually do this methodically... especially if you're dealing with the decimal system (probably with other number systems as well but I don't know how).

Let's say you want to do be able to convert up to 5 digits to words. You can divide the fifth, fourth, third, second, and first digit to determine which number it actually is.

(53424) / 10000 = 5
(53424 - (50000)) / 1000 = 3
(53424 - (50000 + 3000)) / 100 = 4
(53424 - (50000 + 3000 + 400)) / 10 = 2
(53424 - (50000 + 3000 + 400 + 20)) = 4

Given that you know each number (supposing you figure out the pattern above), you can translate that to English numerics which also has a pattern.
Last edited on
This is a bit off topic, but I made this a while ago and all it does is to write back the number you typed not the words though. Just thought I should share with you.

http://ideone.com/bdlRgH
Topic archived. No new replies allowed.