### Number To Words

Hello guys. I am trying to write a program for number to word conversion. I am facing some problems.
If i enter a two digit number, result is ok. But when i enter a three digit number, last two digits are not converted correctly.
I am unable to understand what i am doing wrong. I'll really appreciate your help.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140`` ``````#include #include using namespace std; int countDigits (int); void oneToTwenty (int); void lastTwo (int); int main() { int number; int digits; cout<<"Enter number [1-1000]: "; cin>>number; digits = countDigits(number); cout<<"Digits: "<= 1 && number <= 20) oneToTwenty(number); else if (number >= 20 && number <= 99) lastTwo(number); } else if (digits ==3) { int firstDigit = number/100; oneToTwenty(firstDigit); cout<<" Hunder "; int lastDigits = (int)((number/100)-firstDigit); lastTwo(lastDigits); } getche(); return 0; } int countDigits(int number) { if (number < 10) { return 1; } int count = 0; while (number > 0) { number /= 10; count++; } return count; } void oneToTwenty (int number) { switch (number) { 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; case 10: cout<<"Ten"; break; case 11: cout<<"Eleven"; break; case 12: cout<<"Twelve"; break; case 13: cout<<"Thirteen"; break; case 14: cout<<"Fourteen"; break; case 15: cout<<"Fifteen"; break; case 16: cout<<"Sixteen"; break; case 17: cout<<"Seventeen"; break; case 18: cout<<"Eighteen"; break; case 19: cout<<"Nineteen"; break; case 20: cout<<"Twenty"; break; } } void lastTwo (int number) { int firstDigit = number/10; int secondDigit = number%10; if (firstDigit == 2) cout<<"Twenty "; else if (firstDigit == 3) cout<<"Thirty "; else if (firstDigit == 4) cout<<"Forty "; else if (firstDigit == 5) cout<<"Fifty"; else if (firstDigit == 6) cout<<"Sixty "; else if (firstDigit == 7) cout<<"Seventy "; else if (firstDigit == 8) cout<<"Eighty "; else cout<<"Ninety "; oneToTwenty(secondDigit); }``````
The problem is on line 29. The calculation of `lastDigits` is wrong.
line 29 will always be 0. it is (number/100)-(number/100)

so remove line 29 and modify line 120 a bit:
`int firstDigit = (number/10)%10;`
Last edited on
closed account (j3Rz8vqX)
I've modified these lines:
line 28, 29:
 ``12`` `````` cout<<" Hundred "; int lastDigits = number%100;``````

line 137:
 `` `` `` else if (firstDigit == 9)``

And it should work, unless I've forgot to mention a change.
Oh what a mistake :).. Thank you guys. Everything is good now.
Topic archived. No new replies allowed.