please help me :)

Guys i need help.. This program is supposed to convert 3 digit numbers to words.. like 123 to "one hundred twenty three".. my only problem is with eleven - nineteen.. ex.. if i type 512.. it only shows "five hundred two" 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
#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]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
 
        if(n == 1000)
        {
                cout<<"One Thousand";
                cout<<endl;
        }
        else 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/100 >= 1) //STARTING FROM HERE
        {
          cout<<ones[n/100]<<" Hundred ";
          if (n%100 >= 20 || n%100 <= 99)
          {
                   cout<<tens[(n%100)/10]<<" "<<ones[n%10];
          }
          else if (n%100 <= 19 || n%100 >= 10)
          {
                   cout<<teens[n%10];
          }
          cout<<endl;
        }
        else
        {
                cout<<tens[n/10];
                if(n%10 > 0)
                {
                        cout<<" "<<ones[n%10];
                }
                cout<<endl;
        }
}
Please do not double post. You have another thread which is oly two positions lower than that: http://www.cplusplus.com/forum/beginner/102276/
54
55
56
57
          if (n%100 >= 20 || n%100 <= 99)
          {
                   cout<<tens[(n%100)/10]<<" "<<ones[n%10];
          }
512%100 == 12, 12 <= 99, this if-statement is true. I think you meant &&, not ||
i cant delete the other one.. and im not getting replies while the other posts are getting many replies.. Xl
That's no reason to be impatient.

Also, you missed my reply - I explained what is wrong with your logic.
sorry guys.. Xl This was irritating me for a long time and that might made me a little impatient.. Xl sorry again.. L B i tried what you said.. it worked but now when i type 501 it only shows "five hundred"..
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        else if (n/100 >= 1) //STARTING FROM HERE
        {
          cout<<ones[n/100]<<" Hundred ";
          if (n%100 >= 20 && n%100 <= 99) //between 20 and 99
          {
                   cout<<tens[(n%100)/10]<<" "<<ones[n%10];
          }
          else if (n%100 <= 19 && n%100 >= 10) //between 10 and 19
          {
                   cout<<teens[n%10];
          }
          //between 1 and 9...?
          cout<<endl;
        }
OH GOD.. i finally got it!! X) i added this
1
2
3
4
          else if (n%100 >= 1 && n%100 <=9)
          {
               cout<<ones[n%10];
          }

after the teens... thank you L B and AbstractionAnon for helping me.. :) sorry again for being impatient..
Topic archived. No new replies allowed.