QUESTION: Convert numbers to words

Create a program that prompts the user to enter a number (0-100000 only) then output the number in words.

Enter a number: 10012
it outputs " ten thousand two" instead of ten thousand twelve...
can you please tell me what are my errors ?

im also having a problem with the 11-19 ten thousandths case ...

here's my program...

#include <iostream>

using namespace std;

void main()
{

int num,ones,tenths,hundredths,thousandths,tenthousandths,hundredthousandths;

cout<<"Enter a Number: ";
cin>>num;

if (num==0)
cout<<"zero";
if (num>=0&&num<=100000)
{
hundredthousandths=num/100000;

switch (hundredthousandths)
{
case 1:
cout<<" one hundred thousand ";
break;
}

tenthousandths=num/10000%10;

switch (tenthousandths)
{
case 0:
cout<<" ";
break;
case 1:
cout<<"ten";
break;
case 2:
cout<<"twenty";
break;
case 3:
cout<<"thirty";
break;
case 4:
cout<<"forty";
break;
case 5:
cout<<"fifty";
break;
case 6:
cout<<"sixty";
break;
case 7:
cout<<"seventy";
break;
case 8:
cout<<"eighty";
break;
case 9:
cout<<"ninety";
break;
}

thousandths=num/1000%10;

switch (thousandths)
{
case 0:
cout<<" thousand ";
break;
case 1:
cout<<" one thousand ";
break;
case 2:
cout<<" two thousand ";
break;
case 3:
cout<<" three thousand ";
break;
case 4:
cout<<" four thousand ";
break;
case 5:
cout<<" five thousand ";
break;
case 6:
cout<<" six thousand ";
break;
case 7:
cout<<" seven thousand ";
break;
case 8:
cout<<" eight thousand ";
break;
case 9:
cout<<" nine thousand ";
break;
}


hundredths=num/100%10;

switch (hundredths)
{
case 1:
cout<<"one hundred ";
break;
case 2:
cout<<"two hundred ";
break;
case 3:
cout<<"three hundred ";
break;
case 4:
cout<<"four hundred ";
break;
case 5:
cout<<"five hundred ";
break;
case 6:
cout<<"six hundred ";
break;
case 7:
cout<<"seven hundred ";
break;
case 8:
cout<<"eight hundred ";
break;
case 9:
cout<<"nine hundred ";
break;
}


tenths=num/10%10;

switch (tenths)
{
case 2:
cout<<"twenty ";
break;
case 3:
cout<<"thirty ";
break;
case 4:
cout<<"forty ";
break;
case 5:
cout<<"fifty ";
break;
case 6:
cout<<"sixty ";
break;
case 7:
cout<<"seventy ";
break;
case 8:
cout<<"eighty ";
break;
case 9:
cout<<"ninety ";
break;

}

ones=num%10;
if(ones>=1&&ones<=19)

{
switch (ones)
{
case 0:
cout<<" ";
break;
case 1:
cout<<"one";
break;
case 2:
cout<<"two";
break;
case 3:
cout<<"three";
break;
case 4:
cout<<"four";
break;
case 5:
cout<<"five";
break;
case 6:
cout<<"six";
break;
case 7:
cout<<"seven";
break;
case 8:
cout<<"eight";
break;
case 9:
cout<<"nine";
break;
case 10:
cout <<"Ten";
break;
case 11:
cout <<"Eleven";
break;
case 12:
cout <<"Twelve";
break;
case 13:
cout <<"Thirteen";
break;
case 14:
cout <<"Fourteen";
break;
case 15:
cout <<"Fifteen";
break;
case 16:
cout <<"Sixteen";
break;
case 17:
cout <<"Seventeen";
break;
case 18:
cout <<"Eighteen";
break;
case 19:
cout <<"Nineteen";
break;
default:
cout <<" Error ";
}
}
cout<<endl;
}
}


thanks
You're going to have to test num two digits at a time for the 10/1 place, and the 10,000/1,000 place. You'll need a bigger switch statement.

If I were doing this problem, I'd create a routine to break down three places at a time.
If you're up to speed with functions, I'd split main() up into functions.

And then note that your thousand, hundred, units switch statements are almost identical. Except for the "thousand" and "hundred".

I'm not sure what to call the function, but 12000 should result in the call

displayNumber(12, "thousand"); --> "twelve thousand"

and just 12

displayNumber(12, ""); --> "twelve"

Then you'll be able to reuse the code that converts 314 to "three hundred and fourteen" more easily!

To avoid some of your switch statements getting too long, you might need a second level of condition testing. If value < 20, handle one way. Otherwise handle as currently.

Andy
Last edited on
I see a basic problem in your logic... When you do "num%10" for the ones place, (if you had put it in code format, I would have indicated line number), how can you possibly get a number greater than 9? That is why you're having a problem with 11-19... You need to have more cases, considering the values 11-19... then from 20 on, its standard...
Last edited on
Topic archived. No new replies allowed.