can you help me on how to convert the numbers into words .
here is my example on how to do that program but im not sure if this is a correct program..
#<iostream.h>
#<conio.h>

int ones,tens,hundreds
main()
{
cout<<"ENTER NUMBER";
cin>>number;
cout<<"ENTER ONES";
cin>>ONES;
if(number==1);
cout<<"one";
else if(number==2);
cout<<"two";
else if(number==3);
cout<<"three";
else if(number==4);
cout<<"four";
else if(number==5);
cout<<"five";

ang so on and so forth ,,,

--this program that i make is too long ..how can i make a short program?help me ..

Recommended Answers

All 6 Replies

Use arrays for the strings.

Your comment could have been more descriptive and helpful.

Use arrays for the strings.

By building on what WaltP said, I'm afraid even if you use an array for the strings, ur code will still be long, although not as sequential as you posted it. Solutions I can think of may be optimal but still not short.

look at similar threads.
try to think to implement a class

I think that the first thing to do is to get a container for all the unique words that describe numbers (not compounded). If in English, that's not very hard or very many. The unique numbers run from 0 to 19, then 20-30-40..90 and 100-1000-1000000.., everything else is a compounded of these unique words. I would suggest you use a std::map<int, std::string> to store the associations of the numbers with the corresponding word, but only for those unique numbers. As so:

#include <map>

int main() {
  std::map<int, std::string> num_words;
  num_words[0] = "zero";
  num_words[1] = "one";
  num_words[2] = "two";
  ...
  num_words[19] = "nineteen";
  num_words[20] = "twenty";
  num_words[30] = "thirty";
  ...
  num_words[90] = "ninety";
  num_words[100] = "hundred";
  num_words[1000] = "thousand";
  num_words[1000000] = "million";
  num_words[1000000000] = "billion";
  ....

};

At this point, you can devise grammar rules to compound those number-words to construct any given number (up to some maximum). The first rule could be:

cout<<"ENTER NUMBER";
  cin>>number;

  if( number < 21 ) 
    cout << num_words[number]; //all numbers from 0 to 20 are not compounded.
  else if( number < 100 ) {
    cout << num_words[ (number / 10) * 10 ]; //extract the ten-multiple.
    if( number % 10 != 0 )
      cout << "-" << num_words[ number % 10 ]; //extract the last digit (1 to 9).
  } else ....
  //... so on for more complicated rules.

By building on what WaltP said, I'm afraid even if you use an array for the strings, ur code will still be long, although not as sequential as you posted it. Solutions I can think of may be optimal but still not short.

I don't think 70 lines is that long. 38 lines of actual code from int main() to return 0;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.