switch

Create a program that prompts the user to enter a number (0-900only) then output the number in words.

Enter a number: 12
it outputs " twelve"
can you please tell me what are my errors ?

im also having a problem with the 11-19 when i inut 11 it becomes "tenone" instead of eleven

heres my 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
#include<iostream.h>
#include<conio.h>
main()
{int num,ones,tens,hundreds;
clrscr();
cout<<"Enter a digit from 1-3000: ";
       cin>>num;
       
       hundreds=((num/100)%100)%10;
       tens=(num/10)%10;
       ones=num%10;
 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
Please re-post this with the code tags -- you will find them under Format.

Also, I don't see where you handle the case of eleven.
Good call @koothkeeper I think he needs to handle 11-19 (Teens) separately. It is weird those 11-19 seem to be the the weird deviations in how we speak numbers.

I agree @Bdanielz: That number range is weird in other languages too.

http://www.thecrazyprogrammer.com/2013/03/c-program-to-enter-number-and-print-it.html

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

void once(int a)
{
    switch(a)
    {
    case 1: std::cout << "one";
        break;
    case 2: std::cout << "two";
        break;
    case 3: std::cout << "Three";
        break;
    case 4: std::cout << "Four";
        break;
    case 5: std::cout << "Five";
        break;
    case 6: std::cout << "Six";
        break;
    case 7: std::cout << "Seven";
        break;
    case 8: std::cout << "Eight";
        break;
    case 9: std::cout << "Nine";
        break;
    }
}

int tens(int a,int b)
{
    int flag=0;
    switch(a)
    {
    case 1:
        flag=1;
        switch(b)
        {
        case 0: std::cout << "Ten";
            break;
        case 1: std::cout << "Eleven";
            break;
        case 2: std::cout << "Twelve";
            break;
        case 3: std::cout << "Thirteen";
            break;
        case 4: std::cout << "Fourteen";
            break;
        case 5: std::cout << "Fifteen";
            break;
        case 6: std::cout << "Sixteen";
            break;
        case 7: std::cout << "Seventeen";
            break;
        case 8: std::cout << "Eighteen";
            break;
        case 9: std::cout << "Nineteen";
            break;
        }
        break;
    case 2: std::cout << "Twenty";
        break;
    case 3: std::cout << "Thirty";
        break;
    case 4: std::cout << "Fourty";
        break;
    case 5: std::cout << "Fifty";
        break;
    case 6: std::cout << "Sixty";
        break;
    case 7: std::cout << "Seventy";
        break;
    case 8: std::cout << "Eighty";
        break;
    case 9: std::cout << "Ninety";
        break;
    }
    return(flag);
}

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

int main()
{
    //clrscr();
    int n,a[3],i=0,flag=0;
    std::cout << "Enter any number(max 3 digits):";
    std::cin>>n;

    while(n!=0)
    {
        a[i++]=n%10;
        n=n/10;
    }

    for(i = i-1; i >= 0; i--)
    {
        if(i==0&&flag==0)
        once(a[0]);
        if(i==1)
        flag=tens(a[1],a[0]);
        if(i==2)
        hundred(a[2]);
        std::cout << " ";
    }
    getch();
}

but we are not aplicable to use if/else statement i really dont know what to do
Use arrays to store the words. For example:
1
2
3
4
5
6
7
    static string ones[] =
	{"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "};
    static string teens[] =
	{"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};

    static string tens[] =
	{"twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "};


Then just accumulate the words for large values and reduce the number. This fragment will do up to 99. Note that I made zero a special case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    string result;

    if (n == 0) {
	result = "zero ";
    }

    if (n >= 20) {
	result += tens[(n / 10) - 2];
	n %= 10;
    }
    if (n >= 10) {
	result += teens[n-10];
	n = 0;
    }
    if (n > 0) {
	result += ones[n-1];
    }

    // The value ends with a space, trim it off.
    result.resize(result.size()-1);
    return result;


This should give you an idea of how to add code to deal with hundreds. This is part of a funciton I have that converts 64 bit numbers to words.
Topic archived. No new replies allowed.