PLEASE HELP.. only one more problem

Guys i need help.. This program is supposed to convert 3 digit numbers to words.. like 123 to "one hundred twenty three".. but when i type 123 it will only show "one hundred three".. i think that it is stuck in the part that i commented in the code.. I dont know how to get out of that part!! help? :) please? :>

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
 #include <iostream>
#include <string>
using namespace std;
void ConvertToWords(int n);
 
int main()
{
        int num;
 
        cout<<"Imput da freakin' numbah: ";
        cin>>num;
       
        cout<<"\n";
 
        ConvertToWords(num);
 
        system("Pause");
        return 0;
}
 
void ConvertToWords(int n)
{
        const char *ones[10]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
        const char *teens[10]={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
        const char *tens[10]={"Ghost","Ghost 2","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
 
        if(n < 0 || n > 1000)
        {
                cout<<"DAT IS NAT A NUMBAH!!"<<endl;
        }
        else if(n <= 9)
        {
                cout<<ones[n%10];
                cout<<endl;
        }
        else if(n <= 19 && n > 9)
        {
                cout<<teens[n%10];
                cout<<endl;
        }
        else if(n%100 == 0)
        {
                cout<<ones[n/100]<<" Hundred";
                cout<<endl;
        }
        else if(n > 99) //hundreds
        {
                cout<<ones[n/100]<<" Hundred ";
                if(n%100 >= 1 || n%100 <= 9)
                {
                        cout<<ones[n%10];
                }
                else if(n/100 < 20 || n/100 > 9)
                {
                        cout<<teens[n%100];
                }
                else
                {
                        cout<<" "<<tens[n%100]<<" "<<ones[n%100];
                }
                cout<<endl;
        }
        else if(n == 1000)
        {
                cout<<"One Thousand";
        }
        else
        {
                cout<<tens[n/10];
                if(n%10 > 0)
                {
                        cout<<" "<<ones[n%10];
                }
                cout<<endl;
        }
}
Last edited on
It would be easier if you went from thousands to ones.

Extract the thousands digit, if non-zero output that. Extract the hundreds digit, output that, then move on to the tens and ones digits.
hmmm.. i kinda get what youre saying.. ill try and code it and post here again..
Topic archived. No new replies allowed.