SOURCE FILE: EX0413.CPP


//   
// Example 4.13     
// Program to print a one- or two-digit positive integer in words   
//    
   
#include <iostream.h>
   
void PrintNumber(int n);   
void PrintUnits(int UnitsDigit);   
void PrintTens(int TensDigit);   
void PrintTeens(int UnitsDigit);   
   
int main(void)   
{   
   int n;      
   
   cout << "Enter a positive integer less than 100: ";
   cin >> n;
   if ((n < 1) || (n > 99))   
      cout << "Sorry, your number is out of range.";
   else   
      PrintNumber(n);   
   cout << endl;
   
   return 0;
}   
   
//    
// Function to print a positive integer in words   
// Pre:  n is an integer satisfying 0 < n < 100.   
// Post: n has been printed in words.   
//    
    
void PrintNumber(int n)   
{   
   int UnitsDigit,      // units digit 
       TensDigit;         // tens digit  
          
   UnitsDigit = n % 10;   
   TensDigit  = n / 10;   
   
   if (TensDigit == 0)   
      PrintUnits(UnitsDigit);   
   else if (TensDigit == 1)   
      PrintTeens(UnitsDigit);   
   else   
      {   
         PrintTens(TensDigit);   
         if (UnitsDigit > 0)   
         {   
            cout << "-";
            PrintUnits(UnitsDigit);   
         }   
      }   
}   
   
//   
// Function to print a word for the units digit   
// Pre:  UnitsDigit is an integer with 0 < UnitsDigit < 10.   
// Post: UnitsDigit has been printed in a word.   
//    
         
void PrintUnits(int UnitsDigit)   
{   
   switch (UnitsDigit)   
   {   
      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";
   }   
}   
   
//   
// Function to print a word for the tens digit   
// Pre:  TensDigit is an integer with 1 < TensDigit < 10.   
// Post: TensDigit has been printed in a word.   
//    
         
void PrintTens(int TensDigit)   
{   
   switch (TensDigit)   
   {   
      case 2:   cout << "twenty";
            break;   
      case 3:   cout << "thirty";
            break;   
      case 4:   cout << "forty";
            break;   
      case 5:   cout << "fifty";
            break;   
      case 6:   cout << "sixty";
            break;   
      case 7:   cout << "seventy";
            break;   
      case 8:   cout << "eighty";
            break;   
      case 9:   cout << "ninety";
   }   
}   
   
//   
// Function to print a number in the teens using words   
// Pre:  UnitsDigit is an integer with 0 <= UnitsDigit < 10.   
// Post: Corresponding item has been printed in a word.   
//    
         
void PrintTeens(int UnitsDigit)   
{   
   switch (UnitsDigit)   
   {   
      case 0:   cout << "ten";
            break;   
      case 1:   cout << "eleven";
            break;   
      case 2:   cout << "twelve";
            break;   
      case 3:   cout << "thirteen";
            break;   
      case 4:   cout << "fourteen";
            break;   
      case 5:   cout << "fifteen";
            break;   
      case 6:   cout << "sixteen";
            break;   
      case 7:   cout << "seventeen";
            break;   
      case 8:   cout << "eighteen";
            break;   
      case 9:   cout << "nineteen";
   }   
}