convert number to words using irritation

How to convert numbers to word using irritation? Here's the source code that convert num to words but it uses if.. else statement. i want to use an iteration method. how do i code it?

#include<iostream>
#include<string>
using namespace std;
int expand(int);
int main()
{
int inum;
int terminate= 1;

while(terminate!= 0)
{
cout<<"Enter a number that is less than 0 and equal to -9999999 and less than 1000000: ";
cin>>inum;
expand(inum);

cout<< "\n"<<endl;
cout<< "For running again press 1 or press 0 to exit: \n";
cin>>terminate;
}

}
int expand(int inum)
{
string ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
string tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};



while(inum<-9999999 || inum>9999999)
{
cout<<"The number you entered is greater than 9999999! Please enter a number."<<endl;
cin>>inum;
}

if(inum<0)
{
cout<<"negative ";
expand(-inum);
}
else if(inum>=1000000)
{
expand(inum/1000000);
cout<<" million";
if(inum%1000000)
{
if(inum%1000000<1000)
{
cout<<" ";
}
cout<<" ";
expand(inum % 1000000);
}
}
else if(inum>=1000)
{
expand(inum/1000);
cout<<" thousand";
if(inum % 1000)
{
if(inum % 1000 < 100)
{
cout << " ";
}
cout << " " ;
expand(inum % 1000);
}
}
else if(inum >= 100)
{
expand(inum / 100);
cout<<" hundred";
if(inum % 100)
{
cout << " ";
expand (inum % 100);
}
}
else if(inum >= 20)
{
cout << tens[inum / 10];
if(inum % 10)
{
cout << " ";
expand(inum % 10);
}
}
else
{
cout<<ones[inum];
}
return 0;
}
Last edited on
I'm not familiar with the term «irritation»
i mean iteration.
Last edited on
cout<<"Enter a number that is less than 0 and equal to -9999999 and less than 1000000: ";

Not many of those!


irritation
i mean iteration.

I suspect that you meant "recursion".


I'm not sure that the English language lends itself to easy recursion with numbers (but French is worse) - try writing the numbers from 1 to 200 out longhand and search for rules.

It is almost recursive in multiples of 1000, give or take a few commas vs "and". I should start by finding hundreds, "tens" and "units" and writing a routine that will expand up to 1000; (noting the peculiarities below 20 - perhaps early man extended his counting from his fingers to his toes).


Oh, and please use code tags.
Last edited on
english is not that bad.
you need words for zero to 19 as those are all unique and special.
then you need all the 10s... twenty, thirty, forty, ... ninety
then hundred, thousand, million, billion, .... to however high you want to go.

With that smallish set of words I believe you can represent anything pretty easily.
If you want fractions/decimals, there is a similar ugliness out to 1/100 there much like above. After 100 I think you can either copy the above and add th to them (millionth, billionth, etc) or explicitly define them.

Getting the 'and' in the right place is the only thing left, as we use an extra 'and' ... eg one thousand two hundred AND twenty three...

Irritation might be the best word for it. Its not complicated, just annoying to set up.
Last edited on
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
#include <iostream>
#include <string>
using namespace std;

const string UNITS[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                         "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string TENS[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

//======================================================================

string expandThousand( int n )
{
   string result;
   int hundreds, tens, units;

   hundreds = n / 100;
   n %= 100;
   if ( n < 20 ) 
   {
      tens = 0;
      units = n;
   }
   else
   {
      tens = n / 10;
      units = n % 10;
   }

   if ( hundreds > 0 )
   {
      result += UNITS[hundreds] + " hundred";
      if ( n > 0 ) result += " and ";
   }

   if ( tens > 0 )
   {
      result += TENS[tens];
      if ( units > 0 ) result += "-";
   }

   if ( units > 0 )
   {
      result += UNITS[units];
   }

   return result;
}

//======================================================================

string expand( int n )
{
   string result;
   int millions, thousands, hundreds;

   millions = n / 1000000;
   n %= 1000000;
   thousands = n / 1000;
   n %= 1000;
   hundreds = n / 100;

   if ( millions > 0 )
   {
      result += expandThousand( millions ) + " million";
      if ( thousands > 0 || n > 0 ) result += ( thousands + hundreds > 0 ? ", " : " and " );
   }

   if ( thousands > 0 )
   {
      result += expandThousand( thousands ) + " thousand";
      if ( n > 0 ) result += ( hundreds > 0 ? ", " : " and " );
   }

   if ( n > 0 )
   {
      result += expandThousand( n );
   }

   return result;
}

//======================================================================

int main()
{
   int n;

   while ( true )
   {
      cout << "Enter a number between 1 and 999999999 (or 0 to finish): ";   cin >> n;
      if ( n <= 0 ) break;
      cout << expand( n ) << endl;
   }
}

//====================================================================== 

Enter a number between 1 and 999999999 (or 0 to finish): 999999999
nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
Enter a number between 1 and 999999999 (or 0 to finish): 4096
four thousand and ninety-six
Enter a number between 1 and 999999999 (or 0 to finish): 1000001
one million and one
Enter a number between 1 and 999999999 (or 0 to finish): 0


It's the "and"s, commas and hyphens that are the "irritation".

But it's definitely worse in French.
Last edited on
Topic archived. No new replies allowed.