Having Issues With A Number To Words Converter Program

Hi,

I'm a student taking an intro to programming course and I'm having some trouble with a recent assignment.
The question asks for a program that will take in integers between 0 and 99 and output their, i guess, "English language equivalent". This is what I have.


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
int main()
{
	int num;
	string msg = "The number you entered is ";

	cout << "Enter a number between 0 and 99 inclusive: ";
	cin >> num;
	cout << endl;

	int first_digit = num / 10, second_digit = num % 10;

	if (num > 0 && num < 100)
	{
		switch (num)
		{
			if (num < 20)
			{
		case 0: cout << msg << "zero.\n";
			break;
		case 1: cout << msg << "one.\n";
			break;
		case 2:cout << msg << "two.\n";
			break;
		case 3: cout << msg << "three.\n";
			break;
		case 4: cout << msg << "four.\n";
			break;
		case 5: cout << msg << "five.\n";
			break;
		case 6: cout << msg << "six.\n";
			break;
		case 7: cout << msg << "seven.\n";
			break;
		case 8: cout << msg << "eight.\n";
			break;
		case 9: cout << msg << "nine.\n";
			break;
		case 10: cout << msg << "ten.\n";
			break;
		case 11: cout << msg << "eleven.\n";
			break;
		case 12: cout << msg << "twelve.\n";
			break;
		case 13: cout << msg << "thirteen.\n";
			break;
		case 14: cout << msg << "fourteen.\n";
			break;
		case 15: cout << msg << "fifteen.\n";
			break;
		case 16: cout << msg << "sixteen.\n";
			break;
		case 17: cout << msg << "seventeen.\n";
			break;
		case 18: cout << msg << "eighteen.\n";
			break;
		case 19: cout << msg << "nineteen.\n";
			break;
			}
		}

		switch (first_digit)
		{
			if (first_digit > 1 && second_digit > 0)
		case 2: cout << msg << "twenty-";
			break;
		case 3: cout << msg << "thirty-";
			break;
		case 4: cout << msg << "forty-";
			break;
		case 5: cout << msg << "fifty-";
			break;
		case 6: cout << msg << "sixty-";
			break;
		case 7: cout << msg << "seventy-";
			break;
		case 8: cout << msg << "eighty-";
			break;
		case 9: cout << msg << "ninety-";
			break;
		}

		switch (second_digit)
		{
			if (first_digit > 1 && second_digit > 0)
		case 1: cout << "one.\n";
			break;
		case 2: cout << "two.\n";
			break;
		case 3: cout << "three.\n";
			break;
		case 4: cout << "four.\n";
			break;
		case 5: cout << "five.\n";
			break;
		case 6: cout << "six.\n";
			break;
		case 7: cout << "seven.\n";
			break;
		case 8: cout << "eight.\n";
			break;
		case 9: cout << "nine.\n";
			break;
		}
	}
	else
		cout << "Sorry, that is not a number between 0 and 99.\n";

	cout << "Thank you for using David's Number Interpreter!\n";
}


Not the most concisely written code ever, but done to the best of my elementary knowledge. EITHER WAY, I'm having an issue where if the user inputs, for example, 2, it will output both "The number you have entered is two" AND "two.", the latter of which is meant to only be printed if the number is two digits long.

I tried uploading the source file to make things easier but it wont permit me.

Thanks in advance!
David
closed account (48T7M4Gy)
The reason is very simple. You need to break out of the first switch loop after it is complete instead of continuing on to the next two. You can test this by making a "twoa" and "twob" message string.

1
2
3
switch (num)
		etc etc
		case 2:cout << msg << "twoA.\n";


1
2
3
switch (second_digit)
		etc etc
		case 2:cout << msg << "twoB.\n";

Last edited on
That's what I figured, but how would I break out of the first switch loop?
closed account (48T7M4Gy)
There are probably many ways but one that immediately springs to mind is a cascade that only uses the 'num switch' for numbers between 0 and 9 anything bigger will be handled by the others. This entails a small modification including to line 12.

:)
Topic archived. No new replies allowed.