I've reached a point in "Jumping into C++" where I need to make a program that converts input numbers into their word equivalent.
So far I've made it work for numbers 0-9999. I've tried implementing 10000-99999 but there are problems with the order of the words printed (57865 would print fifty thousand seven thousand eight hundred sixty five). But besides that, the program is absolutely enormous (for me) and I'm wondering if it can be shortened. Keep in mind I can only use loops and if statements so far. Here it is:
Code:#include <iostream> #include <string> void extras(int e); void digits(int x); void tens(int xx); void hundreds(int xxx); void thousands(int xxxx); //void tens_of_thousands(long xxxxx); using namespace std; string zero="zero"; string one="one"; string two="two"; string three="three"; string four="four"; string five="five"; string six="six"; string seven="seven"; string eight="eight"; string nine="nine"; string ten="ten"; string eleven="eleven"; string twelve="twelve"; string thirteen="thirteen"; string fourteen="fourteen"; string fifteen="fifteen"; string sixteen="sixteen"; string seventeen="seventeen"; string eighteen="eighteen"; string nineteen="nineteen"; string twenty="twenty"; string thirty="thirty"; string forty="forty"; string fifty="fifty"; string sixty="sixty"; string seventy="seventy"; string eighty="eighty"; string ninety="ninety"; string hundred="hundred"; string thousand="thousand"; int main() { long number; cout<<"Enter number: "; cin>>number; if(number<0) { cout<<"You cannot enter a negative number. Try again"<<endl; cin>>number; } long aux=number; int i=0; if(number>20) { while(aux!=0) { i++; aux=aux/10; } if(i==1) { digits(number); } else if(i==2) { tens(number); } else if(i==3) { hundreds(number); } else if(i==4) { thousands(number); } /* else if(i==5) { tens_of_thousands(number); } */ } else { extras(number); } cin.ignore(); cin.get(); } void extras(int e) { if(e==0) { cout<<zero; } else if(e==1) { cout<<one; } else if(e==2) { cout<<two; } else if(e==3) { cout<<three; } else if(e==4) { cout<<four; } else if(e==5) { cout<<five; } else if(e==6) { cout<<six; } else if(e==7) { cout<<seven; } else if(e==8) { cout<<eight; } else if(e==9) { cout<<nine; } else if(e==10) { cout<<ten; } else if(e==11) { cout<<eleven; } else if(e==12) { cout<<twelve; } else if(e==13) { cout<<thirteen; } else if(e==14) { cout<<fourteen; } else if(e==15) { cout<<fifteen; } else if(e==16) { cout<<sixteen; } else if(e==17) { cout<<seventeen; } else if(e==18) { cout<<eighteen; } else if(e==19) { cout<<nineteen; } else { cout<<twenty; } } void digits(int x) { if(x==1) { cout<<one; } else if(x==2) { cout<<two; } else if(x==3) { cout<<three; } else if(x==4) { cout<<four; } else if(x==5) { cout<<five; } else if(x==6) { cout<<six; } else if(x==7) { cout<<seven; } else if(x==8) { cout<<eight; } else if(x==9) { cout<<nine; } } void tens(int xx) { if(xx<20) { extras(xx); } else if(xx/10==2) { cout<<twenty<<" "; digits(xx%10); } else if(xx/10==3) { cout<<thirty<<" "; digits(xx%10); } else if(xx/10==4) { cout<<forty<<" "; digits(xx%10); } else if(xx/10==5) { cout<<fifty<<" "; digits(xx%10); } else if(xx/10==6) { cout<<sixty<<" "; digits(xx%10); } else if(xx/10==7) { cout<<seventy<<" "; digits(xx%10); } else if(xx/10==8) { cout<<eighty<<" "; digits(xx%10); } else if(xx/10==9) { cout<<ninety<<" "; digits(xx%10); } } void hundreds(int xxx) { if(xxx/100==1) { cout<<one<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==2) { cout<<two<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==3) { cout<<three<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==4) { cout<<four<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==5) { cout<<five<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==6) { cout<<six<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==7) { cout<<seven<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==8) { cout<<eight<<" "<<hundred<<" "; tens(xxx%100); } else if(xxx/100==9) { cout<<nine<<" "<<hundred<<" "; tens(xxx%100); } } void thousands(int xxxx) { if(xxxx/1000==1) { cout<<one<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==2) { cout<<two<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==3) { cout<<three<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==4) { cout<<four<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==5) { cout<<five<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==6) { cout<<six<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==7) { cout<<seven<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==8) { cout<<eight<<" "<<thousand<<" "; hundreds(xxxx%1000); } else if(xxxx/1000==9) { cout<<nine<<" "<<thousand<<" "; hundreds(xxxx%1000); } } /* void tens_of_thousands(long xxxxx) { if(xxxxx/10000==1) { if(xxxxx%1000<1000) { cout<<ten<<" "<<thousand<<" "; hundreds(xxxxx%1000); cout<<" "; } else { cout<<ten<<" "<<thousand<<" "; thousands(xxxxx%10000); } } else if(xxxxx/10000==2) { cout<<twenty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==3) { cout<<thirty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==4) { cout<<forty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==5) { cout<<fifty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==6) { cout<<sixty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==7) { cout<<seventy<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==8) { cout<<eighty<<" "<<thousand<<" "; thousands(xxxxx%10000); } else if(xxxxx/10000==9) { cout<<ninety<<" "<<thousand<<" "; thousands(xxxxx%10000); } } */