convert number into character form in C++

#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,

Download code and execute yourself, 

In the program, five functions are used with file handling and switch statement. All the function members have been publicly declared using the class (convert is class name) in the program. Each of the first three member function, one(), two(), three(), has a function body a switch statement. The same file (with char file name) is also defined in these three functions. Later in these switch statements, data will write/store in the char name file if the switch statement is true. In which the data will be fetched from the file to convert digit into character form in the program.

Program function get_num() which is called from class object (from x) inside main function void main (). The argument entered by get_num(num) will be the value entered by the user. In the body of this function, first that int type is converted to string (from itoa function). Because now it will behave like a string type value, hence its length has been calculated using the strlen() string function. According to this length, some temporary file (x, y, z) is created which removes as soon as the function terminates after use [remove (“x”), remove (“y”), remove (” z ”), by]. In the body of the same function, another function process() is called as required. Functions (one, two, three) are called in the body of the process function which writes the appropriate data in the file char according to the given input. The data will be written according to the given input in the char file in the program. The convert value is stored in the char file itself, so in order to print the convert character form type values in the program, store data has been read in the char file and the statement to remove the file in its next line [remove (“char “)] Is also given. So that in the next execution the file stores the new value itself.

.