CONVERT 1- 3000 TO WORDS USING SWITCH

HERE I USE IF TOO SO I CAN SET A LIMIT

TNX TO SUPERNAL FOR THE CODE HERES THE LINK FOR IT
http://www.cplusplus.com/forum/general/71840/
BUT THE CODE HERE WAS NOT YET FINISH BY SUPERNAL

SO I PUT SOME OF MINE TO FIX THE PROBLEM

TNX TO SALEM C FOR HELPING ME AND OTHERS TNX

HERES THE CODE



#include <iostream>


using namespace std;

main()

{
int num, ones, tens, hundreds, thousands;


a:
cout<<"\t\tConvert Number to Word!\n\n";
cout<<"Input a number from 1-3000: ";
cin>>num;


thousands=(num/1000)%10;
hundreds=((num/100)%100)%10;
tens=(num/10)%10;
ones=num%10;




if (1<=num&&num<=3000)
{

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 1:

switch (ones)
{
case 0:
cout<<"Ten";
goto b;
break;



case 1:
cout<<"eleven ";

goto b;
break;
case 2:
cout<<"twelve ";
goto b;
break;


case 3:
cout<<"thirteen ";
goto b;
break;
case 4:
cout<<"Fourteen ";
goto b;
break;
case 5:
cout<<"Fifteen ";
goto b;
break;
case 6:
cout<<"Sixteen ";
goto b;
break;
case 7:
cout<<"Seventeen ";
goto b;
break;
case 8:
cout<<"Eighteen ";
goto b;
break;
case 9:
cout<<"Nineteen ";
goto b;
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;
}

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;
}

b:

return 0;


}

else
{
cout<<"Invalid/Over the Limit"<<endl;
cout<<"Pls. try again\n"<<endl;
goto a;

}
}





Last edited on
Not a switch/case.
But you can replace arrays with switch/case if desired.
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
#include <iostream>
#include <string>

const std::string units[] = {
    "",         // not used, but keeps words at their index
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",   // not one-teen
    "twelve",   // not two-teen
    "thirteen", // not three-teen
    "fourteen",
    "fifteen",  // not five-teen
    "sixteen",
    "seventeen",
    "eighteen", // not eight-teen
    "nineteen",
};

// Danish would need an entirely different lookup table
// http://www.olestig.dk/dansk/numbers.html
const std::string tens[] = {
    "?!",       // If these appear, it means that the code
    "??",       // to isolate the units broke.
    "twenty",
    "thirty",
    "forty",    // not fourty
    "fifty",    // not fivety
    "sixty",
    "seventy",
    "eighty",   // not eightty
    "ninety",
};

std::string toWords(int n) {
    std::string result;
    if ( n < 20 ) {
        result = units[n];
    } else {
        result = tens[n/10];
        if ( n % 10 != 0 ) {
            result += " ";
            result += units[n%10];
        }
    }
    return result;
}

int main() {
    std::cout<<"Enter a digit from 1-3000: ";
    int num;
    std::cin>>num;
    if (1<=num && num<=3000)
    {
        int t=(num/1000)%10;
        int h=(num/100)%10;
        int u=num%100;
        std::string answer;
        if ( t > 0 ) {
            answer = toWords(t) + " thousand";
        }
        if ( h > 0 ) {
            if ( t > 0 ) answer += ", ";
            answer += toWords(h) + " hundred";
        }
        if ( u > 0 ) {
            if ( t > 0 || h > 0 ) answer += " and ";
            answer += toWords(u);
        }
        std::cout << answer << std::endl;
    }
}


Automate your test process as much as you can.
$ for i in 1 2 5 10 15 20 30 50 100 125 166 222 399 512 1000 1024 1999 2500 3000 ; do echo -n "$i " ; ./a.out <<< $i ; done
1 Enter a digit from 1-3000: one
2 Enter a digit from 1-3000: two
5 Enter a digit from 1-3000: five
10 Enter a digit from 1-3000: ten
15 Enter a digit from 1-3000: fifteen
20 Enter a digit from 1-3000: twenty
30 Enter a digit from 1-3000: thirty
50 Enter a digit from 1-3000: fifty
100 Enter a digit from 1-3000: one hundred
125 Enter a digit from 1-3000: one hundred and twenty five
166 Enter a digit from 1-3000: one hundred and sixty six
222 Enter a digit from 1-3000: two hundred and twenty two
399 Enter a digit from 1-3000: three hundred and ninety nine
512 Enter a digit from 1-3000: five hundred and twelve
1000 Enter a digit from 1-3000: one thousand
1024 Enter a digit from 1-3000: one thousand and twenty four
1999 Enter a digit from 1-3000: one thousand, nine hundred and ninety nine
2500 Enter a digit from 1-3000: two thousand, five hundred
3000 Enter a digit from 1-3000: three thousand


With just a couple of minor tweaks, you can get all the way up to 999999.
Topic archived. No new replies allowed.