Number To Words
Jan 14, 2014 at 1:07pm UTC
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.
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 130 131 132 133 134 135 136 137 138 139 140
#include <iostream>
#include <conio.h>
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: " <<digits<<endl;
if (digits == 1 || digits == 2)
{
if (number >= 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);
}
Jan 14, 2014 at 1:14pm UTC
The problem is on line 29. The calculation of lastDigits
is wrong.
Jan 14, 2014 at 1:20pm UTC
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 Jan 14, 2014 at 1:20pm UTC
Jan 14, 2014 at 1:23pm UTC
I've modified these lines:
line 28, 29:
1 2
cout<<" Hundred " ;
int lastDigits = number%100;
line 137:
else if (firstDigit == 9)
And it should work, unless I've forgot to mention a change.
Jan 14, 2014 at 1:44pm UTC
Oh what a mistake :).. Thank you guys. Everything is good now.
Topic archived. No new replies allowed.