problem with "Convert int 1-3000 to words"

Okay, so our professor gave us a homework about switches and hinted us to use modulus "%". So I came up with the following formula, I checked it and it works but I noticed that I'm lacking the formula for the 11-19 digits. I'm having a hard time thinking for the formula or did I do something wrong on some part? Thanks!

EDIT: Also, it exceeds 3000 (ex: 3001) which isn't supposed to be.

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
#include <iostream>
#include <conio.h>

using namespace std;

main(){
       int num, ones, tens, hundreds, thousands;
       
       cout<<"Enter a digit from 1-3000: ";
       cin>>num;
       
       thousands=(num/1000)%10;
       hundreds=((num/100)%100)%10;
       tens=(num/10)%10;
       ones=num%10;
       
       switch(thousands){
                         case 1:
                              cout<<"One Thousand ";
                              break;
                         case 2:
                              cout<<"Two Thousand ";
                              break;
                         case 3:
                              cout<<"Three Thousand ";
                              break;
                         }
       
       switch (hundreds){
                         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 (tens){
                         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 (ones){
                         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;
                         }
                                 getch();
       }
                                 
Last edited on
I see that you skipped case 1: for tens


1
2
3
4
       switch (tens){
                         case 2:
                              cout<<"Twenty ";
                              break;
Last edited on
Yes, because I'm currently trying out something when I posted it.. I'm confused with what to do. XD

EDIT: I'm thinking that it should read the last two digits for the numbers 10-19.
Because currently, it reads one by one digits. If another switch can read the last two digits and have cases for it then it should be fixed. The problem is how do I get it read the last two digits?

Tried something like this but doesn't work properly.
1
2
3
4
switch ((tens)&&(ones)){
                         case ((1)&&(1)):
                              cout<<"Eleven ";
                              break;
Last edited on
I think you shall use only digits.
Do the teens (11-19) follow the same pattern as everything else? What is different?

So, if (tens == 1), you want to do one thing, but for the other case you want to do something else. Which digits are affected when (tens == 1)?

Does this help?
I've re-added case 1 in switch(tens):
1
2
3
4
switch (tens){
                         case 1:
                              cout<<"Ten ";
                              break;


When I run the code, here are the results:

Enter a digit from 1-3000: 1221
One Thousand Two Hundred Twenty One


Enter a digit from 1-3000: 1211
One Thousand Two Hundred Ten One


Enter a digit from 1-3000: 1213
One Thousand Two Hundred Ten Three


I still can't understand what to do @_@
teens requires a special case. You can't treat them like everything else.

before you go into the tens/ones switches, check to see if the number is in the teens. If it is, have a separate switch for those.
That's my problem, I don't know how to code to check it. I just came up with the formulas to check each digit only not two digits.
It's just an if/else statement.

1
2
3
4
5
6
7
8
if( /* ... number is a teen ... */ )
{
  // do teen special case here
}
else
{
  // do normal tens and ones here
}
Last edited on
Unfortunately, we can't use if else statement, we are only allowed to use switch statements
waaaaat?

Really? You can't use them at all? That's retarded!

I suppose you can simulate an in/else with a switch... but ew.

1
2
3
4
5
6
7
8
switch( tens )
{
case 1:
  // .. do teens special case here

default:
  // .. do normal tens and ones here
}
I know right? Sucks big time.

I remembered our prof told us that possible minimum switches would be 5.
So I guest that I'm on the right way and just lacking the switch for the cases of 10-19.

Correct me if I'm wrong but I thought that putting:
1
2
3
default:
  // .. do normal tens and ones here
}


Wouldn't help, why? because it still reads only ONE digit:
1211


What I need is to make a new switch that will read the last two digits:
1211
So it would be able to sort it if it's in the range of 10-19
Finally our prof allows us to use if/else. I managed to solve the 11-19 problem but if I input a digit like 2345, the output is only "Two Thousand Three Hundred".
It doesn't read the last two digits for some cases


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
#include <iostream>
#include <conio.h>

using namespace std;

main(){
       int num, ones, tens, hundreds, thousands;
       
       cout<<"Enter a number from 1-3000: ";
       cin>>num;
       
       thousands=(num/1000)%10;
       hundreds=((num/100)%100)%10;
       tens=(num/10)%10;
       ones=num%10;
       
       switch(thousands){
                         case 1:
                              cout<<"One Thousand "<<tens;
                              break;
                         case 2:
                              cout<<"Two Thousand ";
                              break;
                         case 3:
                              cout<<"Three Thousand ";
                              break;
                         default:
                                 cout<<"Invalid ";
                                 break;
                         }
       
       switch (hundreds){
                         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;
                         }
                         
                         
       if ((tens == 1) && (ones >= 1)){
                 
                 switch (ones){
                        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 if ((tens >= 1) && (ones == 0)){
            
            switch (tens){
                         case 1:
                              cout<<"Ten ";
                              break;
                         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;
                         }
                         
       }
       
       else if ((tens == 0) && (ones >= 1)){
            
            switch (ones){
                         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;
                         }
            }
                         getch();
       }
                                 
you're not handling (tens > 1) && (ones >= 1).

Logic you might want to consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (tens == 1)
{
   switch (ones)
   {
      ...  // handle all values from 10 - 19
   }
}
else
{
   switch(tens)
   {
      ...
   }

   if (tens > 1) && (ones > 0)
   {
      // print hyphen
   }

   switch (ones)
   {
      ...
   }
}


Last edited on
Topic archived. No new replies allowed.