You are here:

C++/Converting Number into Words

Advertisement


Question
I need help converting numbers into words (C++ program)The input will be -1250 and the output should be minus one two five zero. If it is a positive number the word plus replaces "minus" and if it is a zero, it says zero. Can you help me?

Answer
include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{

int inum;
cout<<"enter the number";
cin>>inum;
int iquo, irem;

iquo = inum/100;
irem = inum%100;
char *cdipp;
cdisp = new char[200];
if( inum < 0)
   {
       strcat(cdisp, "minus");
       inum = 0 - inum;
   }
if(irem >=10 && irem < 20)
{
         switch (irem)
         {
         case 11:
         strcat(cdisp," eleven");

         break;
         case 12:
         strcat(cdisp," twelve");
         
         break;
         case 13:
         strcat(cdisp," thirteen");
         
         break;
         case 14:
         strcat(cdisp," fourtine");

         break;
         case 15:
         strcat(cdisp," fifteen");
         
         break;
         case 16:
         strcat(cdisp," sixteen");
         
         break;
         case 17:
         strcat(cdisp," seventeen");
         
         break;
         case 18:
         strcat(cdisp," eighteen" );
         
         break;
         case 19:
         strcat(cdisp," nineteen");
         
         break;
         }
   }
   if(irem >= 20)
   {
         switch (irem)
         {
         case 20:
         strcat(cdisp," twenty");

         break;
         case 30:
         strcat(cdisp," thirty");
         
         break;
         case 40:
         strcat(cdisp," fourty");
         
         break;
         case 50:
         strcat(cdisp," fifty");

         break;
         case 60:
         strcat(cdisp," sixty");
         
         break;
         case 70:
         strcat(cdisp," seventy");
         
         break;
         case 80:
         strcat(cdisp," eighty");
         
         break;
         case 90:
         strcat(cdisp,"ninenty" );
         
         break;
         }
         irem = irem %10;
         switch (irem)
         {
         case 1:
         strcat(cdisp," one");

         break;
         case 2:
         strcat(cdisp," two");
         
         break;
         case 3:
         strcat(cdisp," three");
         
         break;
         case 4:
         strcat(cdisp," four");

         break;
         case 5:
         strcat(cdisp," five");
         
         break;
         case 6:
         strcat(cdisp," six");
         
         break;
         case 7:
         strcat(cdisp," seven");
         
         break;
         case 8:
         strcat(cdisp," eight" );
         
         break;
         case 9:
         strcat(cdisp," nine" );
         
         break;
         }
         
      }
      irem = inum%1000;
      if( irem >= 100)
      {
         int itemp = irem/100
         switch (itemp)
         {
         case 1:
         strcat(cdisp," hundred");

         break;
         case 2:
         strcat(cdisp," twohundred");
         
         break;
         case 3:
         strcat(cdisp," threehundred");
         
         break;
         case 4:
         strcat(cdisp," fourhundred");

         break;
         case 5:
         strcat(cdisp," fifvehundred");
         
         break;
         case 6:
         strcat(cdisp," sixhundred");
         
         break;
         case 7:
         strcat(cdisp," sevenhundred");
         
         break;
         case 8:
         strcat(cdisp," eighthundred" );
         
         break;
         case 9:
         strcat(cdisp," ninehundred" );
         
         break;
         }
       }
      
  strcat(cdisp,"\n");;
  cout<<"the word is " << cdisp;
   
 
 
  system("PAUSE");
  return EXIT_SUCCESS;
}
Hi I have just provided the logic. I have not compiled the program. I have done it till hundred.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.