Output English word from number

Hi, I am new to the forums here, so forgive me if I placed this in the wrong spot. I looked around, but nothing has helped me out much. I'm having a little trouble finding out what's wrong with my code. I just learned about switch statements, so I may be doing something wrong there. I am supposed to write a program that will read in a two digit number and output the number in English. My teacher wants us to use an if statement with a switch that deals with the numbers 10-19 and two more switch statements under else dealing with 20-99. Thanks in advance for the assistance.
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
#include <iostream>

using namespace std;

int main()
{
	int number;
	char digit1 = number / 10;
	char digit2 = number % 10;

	cout << "Please enter a two digit integer: ";
	cin >> number;

	if (digit1 == '1')
	{
	switch (digit2)
	{
		case '0':
			cout << "Ten";
			break;
		case '1':
			cout << "Eleven";
			break;
		case '2':
			cout << "Twelve";
			break;
		case '3':
			cout << "Thirteen";
			break;
		case '4':
			cout << "Fourteen";
			break;
		case '5':
			cout << "Fifteen";
			break;
		case '6':
			cout << "Sixteen";
			break;
		case '7':
			cout << "Seventeen";
			break;
		case '8':
			cout << "Eighteen";
			break;
		case '9':
			cout << "Nineteen";
			break;
	}
	}
	else
	{
	switch (digit1)
	{
		case '2':
			cout << "Twenty";
			break;
		case '3':
			cout << "Thirty";
			break;
		case '4':
			cout << "Fourty";
			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;
	}
	switch (digit2)
	{
		case '1':
			cout << "-one";
			break;
		case '2':
			cout << "-two";
			break;
		case '3':
			cout << "-three";
			break;
		case '4':
			cout << "-four";
			break;
		case '5':
			cout << "-five";
			break;
		case '6':
			cout << "-six";
			break;
		case '7':
			cout << "-seven";
			break;
		case '8':
			cout << "-eight";
			break;
		case '9':
			cout << "-nine";
			break;
		default:
			cout << "";
			break;
	}
	}
	return 0;
}
Last edited on
Your logic is correct. But first, you have to define the two chars digit1 and digit2 after you cin >> number.

Then, digit1 and digit2 should be defined like this:

char digit1 = (number / 10) + '0'; char digit2 = (number % 10) + '0';

That's because digit1 and digit2 are defined as chars.

For example: char c = 65; initializes c to the character whose position (integer value) = 65, that's an 'A'.

try 66 for 'B', 67 for 'C' and so on.

While on the other hand, int n = 'A'; initializes n to the integer value (position) of 'A' which is 65.

Complete 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
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
#include <iostream>

using namespace std;

int main()
{
	int number;
	

	cout << "Please enter a two digit integer: ";
	cin >> number;
        char digit1 = (number / 10) + '0';
	char digit2 = (number % 10) + '0';

	if (digit1 == '1')
	{
	switch (digit2)
	{
		case '0':
			cout << "Ten";
			break;
		case '1':
			cout << "Eleven";
			break;
		case '2':
			cout << "Twelve";
			break;
		case '3':
			cout << "Thirteen";
			break;
		case '4':
			cout << "Fourteen";
			break;
		case '5':
			cout << "Fifteen";
			break;
		case '6':
			cout << "Sixteen";
			break;
		case '7':
			cout << "Seventeen";
			break;
		case '8':
			cout << "Eighteen";
			break;
		case '9':
			cout << "Nineteen";
			break;
	}
	}
	else
	{
	switch (digit1)
	{
		case '2':
			cout << "Twenty";
			break;
		case '3':
			cout << "Thirty";
			break;
		case '4':
			cout << "Fourty";
			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;
	}
	switch (digit2)
	{
		case '1':
			cout << "-one";
			break;
		case '2':
			cout << "-two";
			break;
		case '3':
			cout << "-three";
			break;
		case '4':
			cout << "-four";
			break;
		case '5':
			cout << "-five";
			break;
		case '6':
			cout << "-six";
			break;
		case '7':
			cout << "-seven";
			break;
		case '8':
			cout << "-eight";
			break;
		case '9':
			cout << "-nine";
			break;
		default:
			cout << "";
			break;
	}
	}
	return 0;
}

It works perfectly now. Thank you very much!
Topic archived. No new replies allowed.