Convert numeric value into Character in C++

Creating a Loading Bar in C++
Creating Searching Progress Bar in C++
Creating Printing Progress Bar in C++
Creating Downloading Progress Bar in C++
Creating a Timer using in C++
Sorting Array Elements as Ascending and Descending Order in C++
Searching Element in Array List in C++
Class with Multidimensional Array in C++
Convert digit values into Character in C++


यह program किसी numeric digit को character form में convert करने का एक simple demonstration है-

एक int data-type का memory size 2 byte होता है जो की लगभग 3200 के आसपास तक की संख्याओं को ही store कर सकता है।

जबकि अधिक संख्याओं को store करने के लिए, हमें यहाँ पर long type modifier को use करना होगा इस program का output एक video के रूप में भी दिया गया है –

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h> // strcpy
#include<stdlib.h> //atoi, exit()
#include<stdio.h> // remove


class convert
{
    public: 
      void one(char [],int);
      void two(int);
      void three(char[],int,int);
      void get_num(int);
      void process(char[],int,int);
}x;

void convert::one(char n[10],int c)
{
      ofstream fout("char",ios::app);
     switch(n[c])
    {
        case '1':fout<<"one ";break;
        case '2':fout<<"two ";break;
        case '3':fout<<"three ";break;
        case '4':fout<<"four ";break;
        case '5':fout<<"five ";break;
        case '6':fout<<"six ";break;
        case '7':fout<<"seven ";break;
        case '8':fout<<"eight ";break;
        case '9':fout<<"nine ";break;
     }
    fout.close();
}
void convert::two(int n)
{
    ofstream fout("char",ios::app);
    switch(n)
    {
        case 11:fout<<"eleven ";break;
        case 12:fout<<"twelve ";break;
        case 13:fout<<"thirteen ";break;
        case 14:fout<<"fourteen ";break;
        case 15:fout<<"fifteen ";break;
        case 16:fout<<"sixteen ";break;
        case 17:fout<<"seventeen";break; 
        case 18:fout<<"eighteen ";break;
        case 19:fout<<"nineteen ";break;
    }
   fout.close();
}
void convert::three(char n[10],int no,int c)
{
    ofstream fout("char",ios::app);
i   f(no==10)
    { 
        fout<<"ten ";}
      if(no==20||n[c]=='2')  {  fout<<"twenty ";  }
      if(no==30||n[c]=='3')  {  fout<<"thirty ";    }
      if(no==40||n[c]=='4')  {  fout<<"fourty ";   }
      if(no==50||n[c]=='5')  {  fout<<"fifty ";      }
      if(no==60||n[c]=='6')  {  fout<<"sixty ";     }
      if(no==70||n[c]=='7')  {  fout<<"seventy "; }
      if(no==80||n[c]=='8')  {  fout<<"eighty ";   }
      if(no==90||n[c]=='9')  {  fout<<"ninety "; } fout.close(); } void convert::get_num(int num) { int l; int lt; char c[10],v[10],v3[10]; itoa(num,c,10); l=strlen(c); if(l>2)
    {
        ofstream f1("x");
        ofstream f2("y");
        for(int k=0;k<l;k++)
       {
         if(l==3)
        { 
            lt=2;
           if(k<=0) {
           f1<<c[k];}
           else
           f2<<c[k]; } else if(l>3)
       { 
           lt=3;
         ofstream f5("z");
         if(l==5)
        {
           if(k==0||k==1){
                f1<<c[k];}
            else if(k==2){
              f2<<c[k];}
          else
            f5<<c[3]<<c[4];
        }
       else if(l==4)
      {
         if(k==0){
         f1<<c[k];}
         else if(k==1){
         f2<<c[k];}
         else
         f5<<c[2]<<c[3]; } } } f1.close(); f2.close(); ifstream f3("x"); char ch[20],v1[10]; while(!f3.eof()) { f3.getline(ch,10); strcpy(v1,ch); } f3.close(); ifstream f4("y"); char d[20],v2[10]; while(!f4.eof()) { f4.getline(d,10); strcpy(v2,d); } f4.close(); if(l>3)
    {
        ifstream f8("z");
        char h[20];
        while(!f8.eof())
       {
          f8.getline(h,10);
          strcpy(v3,h);
       } f8.close();
 }

int cn;
if(v2[0]=='0') {  cn=2;  }  else  {  cn=0;  }

typedef char ar[50];
ar op[]={"hundred ","thousand ",""};

  int co=0;
   while(co<lt)
   {
     if(co==0)  {  num=atoi(v1);        strcpy(v,v1);            x.process(v,num,l);
     ofstream fout("char",ios::app);    
      fout<<op[lt-2];        
      fout.close();}
    if(co==1)   {  num=atoi(v2);        strcpy(v,v2);            x.process(v,num,l);}
    if(co==2)   {  num=atoi(v3);        strcpy(v,v3);
    ofstream fout("char",ios::app);
     fout<<op[cn];
     fout.close();
    x.process(v,num,l);}
  co++;}
    remove("y");    remove("x");        remove("z");
 }
 else
   x.process(c,num,l);
}
void convert::process(char v[10],int n,int len)
{
   if(n<=9||n==10||n>=20)
   {
      for(int i=0;i<len;i++) { if(n==10||n>=20)
       {
         if(i==0)
         {
           x.three(v,n,i);
         }
        if(i==1&&v[i]!='0')
        {
           x.one(v,i);
         }
      }
  else
  {
    x.one(v,i);
   }
 }
}
 else
 {
    x.two(n);
 }
}
void main()
{
   
 do {
    clrscr();
    int num;
    cout<<"Enter number: "; cin>>num;
 
   cout<<\n_________________________________________________\n";
   _setcursortype(0);

    if(num>32000||num<1)
     {
      cout<<"Sorry, can't convert...";
      getch();
        exit(0);  
     
   }
   
   x.get_num(num);
   char ch[100];
   ifstream f1("char");
   while(!f1.eof())
  {
     f1.getline(ch,100);
     cout<<strupr(ch);          // converts character into uppercase
   }
    f1.close();
    remove("char");
    getch();
}while(num!=0);
}

OUTPUT

1st execution
Enter number (maximum 32000): 9999
________________________________________
NINE THOUSAND NINE HUNDRED NINETY NINE

2nd execution
Enter number((maximum 32000):1988
________________________________________
ONE THOUSAND NINE HUNDRED EIGHTY EIGHT 

3rd execution
Enter number((maximum 32000): 21879
________________________________________
TWENTY ONE THOUSAND EIGHT HUNDRED SEVENTY NINE 

4th execution
Enter number((maximum 32000): 10000
________________________________________
TEN THOUSAND

This program is a small part of the project given here, which was created to convert up to 32500 numbers in character form. Because a simple int data-type will only store values up to around 3200.

Whereas to store more numbers, we have to use long type of modifier.

Program will execute as given in below video,

Explanation

program में, file handling और switch statement के साथ 5 function का उपयोग किया जाता है। सभी member function को class में public रूप में declare किया गया है। पहले तीन function की function body एक switch statement है। इन 3 functions को एक file से जोड़ा गया है जिसमे condition true होने पर switch statement write होगा एक तरह से result store होगी और आगे चलकर इस file से result को display किया जायेगा जो कि output value होगा।

Download code and execute yourself, convert digit value into character

Leave a Reply

Your email address will not be published.