Converting number into words

I have written a program to display numbers from 0-9999 into words. The program is working properly but I am experiencing unexpected output when I enter numbers like, 415,612,819 (basically all numbers that include 0-19 at the end). The program I have written is in c++ and I am currently beginner in c++.

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
//This program converts number into words
#include<iostream>
using namespace std;
main()
{
	int number,unit,ten,hundred,thousand;
	cout<<"Please enter any number between 0-9999: ";
	cin>>number;
	thousand=number/1000;
	number=number%1000;
	hundred=number/100;
	number=number%100;
	ten=number/10;
	number=number%10;
 	unit=number;
 	if(ten==1)
{
    if(number==1) cout<<"eleven";
    if(number==2) cout<<"twelve";  
    if(number==3) cout<<"thirteen";
    if(number==4) cout<<"fourteen";
    if(number==5) cout<<"fifteen";
    if(number==6) cout<<"sixteen";
    if(number==7) cout<<"seventeen";
    if(number==8) cout<<"eighteen";
    if(number==9) cout<<"ninteen";
 }
	else
	{	 
	if(thousand>=1 && thousand <=9)
	{
		if(thousand==1) cout<<"one thousand";
		if(thousand==2) cout<<"two thousand";
		if(thousand==3) cout<<"three thousand";
		if(thousand==4) cout<<"four thousand";
		if(thousand==5) cout<<"five thousand";
		if(thousand==6) cout<<"six thousand";
		if(thousand==7) cout<<"seven thousand";
		if(thousand==8) cout<<"eight thousand";
		if(thousand==9) cout<<"nine thousand";
	}
	if(hundred>=1 && hundred <=9)
	{
		if(hundred==1) cout<<" one hundred";
		if(hundred==2) cout<<" two hundred";
		if(hundred==3) cout<<" three hundred";
		if(hundred==4) cout<<" four hundred";
		if(hundred==5) cout<<" five hundred";
		if(hundred==6) cout<<" six hundred";
		if(hundred==7) cout<<" seven hundred";
		if(hundred==8) cout<<" eight hundred";
		if(hundred==9) cout<<" nine hundred";
	}
	if(ten>=1 && ten <=9)
	{
		if(ten==1) cout<<" ten";
		if(ten==2) cout<<" twenty";
		if(ten==3) cout<<" thirty";
		if(ten==4) cout<<" fourty";
		if(ten==5) cout<<" fifty";
		if(ten==6) cout<<" sixty";
		if(ten==7) cout<<" seventy";
		if(ten==8) cout<<" eighty";
		if(ten==9) cout<<" ninety";
	}
	if(unit>=1 & unit <=9)
	{
		if(unit==1) cout<<" one";
		if(unit==2) cout<<" two";
		if(unit==3) cout<<" three";
		if(unit==4) cout<<" four";
		if(unit==5) cout<<" five";
		if(unit==6) cout<<" six";
		if(unit==7) cout<<" seven";
		if(unit==8) cout<<" eight";
		if(unit==9) cout<<" nine";
	}
	}
}
a rough fix: I moved the teens to the correct area of the conditions, added a handler for "zero", and corrected the logic for ten, and I think a little additional logic that was not quite right.

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
84
int main()
{
int number,unit,ten,hundred,thousand;
	cout<<"Please enter any number between 0-9999: ";
	cin>>number;

	thousand=number/1000;
	number=number%1000;
	hundred=number/100;
	number=number%100;
	ten=number/10;
	number=number%10;
 	unit=number;

    if(number == 0){ cout << "zero" << endl; return 0;}
	
	{	 
	if(thousand>=1 && thousand <=9)
	{
		if(thousand==1) cout<<"one thousand";
		if(thousand==2) cout<<"two thousand";
		if(thousand==3) cout<<"three thousand";
		if(thousand==4) cout<<"four thousand";
		if(thousand==5) cout<<"five thousand";
		if(thousand==6) cout<<"six thousand";
		if(thousand==7) cout<<"seven thousand";
		if(thousand==8) cout<<"eight thousand";
		if(thousand==9) cout<<"nine thousand";
	}
	if(hundred>=1 && hundred <=9)
	{
		if(hundred==1) cout<<" one hundred";
		if(hundred==2) cout<<" two hundred";
		if(hundred==3) cout<<" three hundred";
		if(hundred==4) cout<<" four hundred";
		if(hundred==5) cout<<" five hundred";
		if(hundred==6) cout<<" six hundred";
		if(hundred==7) cout<<" seven hundred";
		if(hundred==8) cout<<" eight hundred";
		if(hundred==9) cout<<" nine hundred";
	}
	if(ten>=1 && ten <=9)
	{
		if(ten==1 && unit == 0) cout<<" ten";
		if(ten==2) cout<<" twenty";
		if(ten==3) cout<<" thirty";
		if(ten==4) cout<<" fourty";
		if(ten==5) cout<<" fifty";
		if(ten==6) cout<<" sixty";
		if(ten==7) cout<<" seventy";
		if(ten==8) cout<<" eighty";
		if(ten==9) cout<<" ninety";
	}
	if(ten==1) 
{
    if(unit ==1) cout<<" eleven";
    if(unit ==2) cout<<" twelve";  
    if(unit ==3) cout<<" thirteen";
    if(unit ==4) cout<<" fourteen";
    if(unit ==5) cout<<" fifteen";
    if(unit ==6) cout<<" sixteen";
    if(unit ==7) cout<<" seventeen";
    if(unit ==8) cout<<" eighteen";
    if(unit ==9) cout<<" ninteen";
 }
	else
	if(unit>=1 & unit <=9)
	{
		if(unit==1) cout<<" one";
		if(unit==2) cout<<" two";
		if(unit==3) cout<<" three";
		if(unit==4) cout<<" four";
		if(unit==5) cout<<" five";
		if(unit==6) cout<<" six";
		if(unit==7) cout<<" seven";
		if(unit==8) cout<<" eight";
		if(unit==9) cout<<" nine";
	}
	}

}


Last edited on
@moazali, The way you have it, if tens==1 it just prints out the "teens" part and quits. You need to still print the thousands and hundreds, although you don't want to print the tens or ones afterwards if you've printed a "teen".

You could make the program a lot nicer by using a couple arrays to hold the repetitive words (like "one","two",etc) which can easily be accessed by index.

And then there's the problem of ensuring you don't have an extra space at the beginning ....
Last edited on
Topic archived. No new replies allowed.