Extracting digits

I have a homework assignment where I have to read an integer below 1000 and output the integer in terms of string

EX:
Enter a number <1000: INPUT: 405
You entered Four Hundred and Five

I have a problem outputting the words after the hundred section, the integer slot c and b are not out putting the correct words.

EX:
Enter a number <1000: INPUT: 405
You entered Four Hundred Ten

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

#include <iostream>
#include <string>
#include <ctime>

using namespace std; 
const int size = 1000; 

int main()
{
	time_t p;
	time(&p);

	cout << ctime(&p) << endl;

	int num;

	int a, b, c;

	cout << "Enter a number <1000: " << endl;
	cin >> num;

	if (num >= 1 && num <= 999)

	{
		a = num % 10;
		num /= 10;
		b = num % 10;
		num /= 10;
		c = num % 10;
		num /= 10;

	}

	else
	{

		cout << "You have entered an invalid output!";
		exit(1);
	}
	cout << "You entered ";
	switch (c)
	{

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

	switch (b)
	{
	case(0) : if (a == 0)
	{
				  cout << "Ten.";
	}
			  else
				  cout << "";
			  break;

	case(1) : if (a == 1)
	{
				  cout << "Eleven.";

	}
			  else if (a == 2)
			  {
				  cout << "Tweleve.";
			  }
			  else if (a == 3)
			  {
				  cout << "Thirteen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Fourteen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Fifteen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Sixteen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Seventeen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Eighteen.";
			  }
			  else if (a == 3)
			  {
				  cout << "Nineteen.";
			  }
		break;
	case(2) : cout << "Twenty  "; break;
	case(3) : cout << "Thirty "; break;
	case(4) : cout << "Forty  "; break;
	case(5) : cout << "Fifty  "; break;
	case(6) : cout << "Sixty "; break;
	case(7) : cout << "Seventy "; break;
	case(8) : cout << "Eighty "; break;
	case(9) : cout << "Ninety "; break;
		break;

	}
	
	switch (a)
{
	case(0) : if (b == 1)
	{
				  cout << ""; 
	}
			  else
			  {
				  cout << "";
			  }

		break;
	case(1) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "One. ";
		break;
	case(2) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Two. ";
			  break;
	case(3) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Three. ";
			  break;
	case(4) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Four. ";
			  break;
	case(5) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Five. ";
			  break;
	case(6) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Six. ";
			  break;
	case(7) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Seven. ";
			  break;
	case(8) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Eight. ";
			  break;
	case(9) : if (b == 1)
	{
				  cout << "";
	}
			  else if (b == 0)
			  {
				  cout << "";
			  }
			  else
				  cout << "Nine. ";
			  break;
}

	
	system("PAUSE");
	return 0;




}
Line 59 says "b==0 and a==0", but prints "Ten".

Think about this:
1
2
3
4
5
6
7
8
9
10
11
12
std::string digits[10] { "", "one", "two", "three" };
std::string teens[10] { "ten", "eleven", "twelve" };
if ( 0 < c ) std::cout << digits[c] << " hundred";
if ( 1 == b ) {
  std::cout << ' ' << teens[a];
}
else {
  // print tens with b

  if ( 0 < a ) std::cout << ' ' << digits[a];
}
std::cout << '\n';

The digits is a lookup table that converts numeric index into text. Your switch statements are similar lookup tables, but which might be easier to get right?
Last edited on
It seems a lot easier using an array, but I have to use switch statements and if else statements.


I still can't get it right, I'm sure my condition statements are correct, if the middle integer = 0, and the last digit equals 0, print out "" which is essentially nothing.

if the middle integer is 1 and the last digit is 0, print out ten.

I can't figure out where I went wrong
Can you do this then?
1
2
3
4
5
6
7
8
9
10
11
12
// the hundreds switch

if ( 1==b )
{
  // the teens switch
}
else
{
  // the tens switch

  // the ones switch
}
Yes I can, that seems like such an easy solution, thank you keskiverto!
Topic archived. No new replies allowed.