Displaying numbers as words

I wrote this code to turn numbers into words, however when I enter the three-digit number it fails to print out the tens place. I tried changing the order of the declared identifiers and the switch statements but it still skips on printing out the corresponding word for the tens. If I type in 248, for example, the output comes out to be "Two Hundred Eight". Are the identifiers declared correctly? If, not what can I do to fix it?


#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int number;
cout<<"Enter an integer number< 1000:";
cin>>number;

int hundreds=number/100;
int tens=number/10;
int ones=number%10;

switch (hundreds)
{
case (9):
cout<<"Nine Hundred ";
break;
case (8):
cout<<"Eight Hundred ";
break;
case (7):
cout<<"Seven Hundred ";
break;
case (6):
cout<<"Six Hundred ";
break;
case (5):
cout<<"Five Hundred ";
break;
case (4):
cout<<"Four Hundred ";
break;
case (3):
cout<<"Three Hundred ";
break;
case (2):
cout<<"Two Hundred ";
break;
case (1):
cout<<"One Hundred ";
break;
}

switch (tens)
{
case (9):
cout<<"Ninety ";
break;
case (8):
cout<<"Eighty ";
break;
case (7):
cout<<"Seventy ";
break;
case (6):
cout<<"Sixty ";
break;
case (5):
cout<<"Fifty ";
break;
case (4):
cout<<"Fourty ";
break;
case (3):
cout<<"Thirty ";
break;
case (2):
cout<<"Twenty ";
break;
case (1):
{if (ones==1)
cout<<"Eleven";
else if (ones==2)
cout<<"Twelve";
else if (ones==3)
cout<<"Thirteen";
else if (ones==4)
cout<<"Fourteen";
else if (ones==5)
cout<<"Fifteen";
else if (ones==6)
cout<<"Sixteen";
else if (ones==7)
cout<<"Seventeen";
else if (ones==8)
cout<<"Eighteen";
else
cout<<"Nineteen";
break;}
}

switch (ones)
{
case (9):
cout<<"Nine";
break;
case (8):
cout<<"Eight";
break;
case (7):
cout<<"Seven";
break;
case (6):
cout<<"Six";
break;
case (5):
cout<<"Five";
break;
case (4):
cout<<"Four";
break;
case (3):
cout<<"Three";
break;
case (2):
cout<<"Two";
break;
case (1):
cout<<"One";
break;
}

cout<<endl;
return 0;
}
@daniela93

Here's the corrected 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Numbers As Words.cpp : main project file.
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	int number=1000;
	do
	{
	cout<<"Enter an integer number< 1000:";
	cin>>number;
	} while (number < 1 || number >=1000); // Check for number to be >=1 and < 1000

	int hundreds=number/100;
	int tens=(number-(hundreds*100))/10;
	int ones=number%10;
	
	switch (hundreds)
	{
	case (9):
		cout<<"Nine Hundred ";
		break;
	case (8):
		cout<<"Eight Hundred ";
		break;
	case (7):
		cout<<"Seven Hundred ";
		break;
	case (6):
		cout<<"Six Hundred ";
		break;
	case (5):
		cout<<"Five Hundred ";
		break;
	case (4):
		cout<<"Four Hundred ";
		break;
	case (3):
		cout<<"Three Hundred ";
		break;
	case (2):
		cout<<"Two Hundred ";
		break;
	case (1):
		cout<<"One Hundred ";
		break;
	}

	switch (tens)
	{
	case (9):
		cout<<"Ninety ";
		break;
	case (8):
		cout<<"Eighty ";
		break;
	case (7):
		cout<<"Seventy ";
		break;
	case (6):
		cout<<"Sixty ";
		break;
	case (5):
		cout<<"Fifty ";
		break;
	case (4):
		cout<<"Fourty ";
		break;
	case (3):
		cout<<"Thirty ";
		break;
	case (2):
		cout<<"Twenty ";
		break;

	case (1):
		{
			if (ones==1)
			{
				cout<<"Eleven";
				break;
			}
			else if (ones==2)
			{
				cout<<"Twelve";
				break;
			}
			else if (ones==3)
			{
				cout<<"Thirteen";
				break;
			}
			else if (ones==4)
			{
				cout<<"Fourteen";
				break;
			}
			else if (ones==5)
			{
				cout<<"Fifteen";
				break;
			}
			else if (ones==6)
			{
				cout<<"Sixteen";
				break;
			}
			else if (ones==7)
			{
				cout<<"Seventeen";
				break;
			}
			else if (ones==8)
			{
				cout<<"Eighteen";

				break;
			}else if (ones==9)
			{
				cout<<"Nineteen";
				break;
			}
			else
				break;
		}

	}
	if (tens>1 || number <10) // if number greater than 19 or number less than 10
	{
		switch (ones)
		{
		case (9):
			cout<<"Nine";
			break;
		case (8):
			cout<<"Eight";
			break;
		case (7):
			cout<<"Seven";
			break;
		case (6):
			cout<<"Six";
			break;
		case (5):
			cout<<"Five";
			break;
		case (4):
			cout<<"Four";
			break;
		case (3):
			cout<<"Three";
			break;
		case (2):
			cout<<"Two";
			break;
		case (1):
			cout<<"One";
			break;
		}
	}
	cout<<endl;
	return 0;
} 
Topic archived. No new replies allowed.