-
convert numbers to words
hello... I am trying to convert 2 digit numbers to words: example 11= eleven.
so far I have:
Code:
#include <iostream>
using namespace std;
cout<<"Enter a two-digit number: ";
cin>>number;
if(number>99)
{
cout<<"Number out of range";
exit(0);
}
if(number==0)
{
cout<<"Zero";
exit(0);
}
temp=number;
digit=number%10;
last_two=digit;
number=number/10;
digit=number%10;
last_two=(digit*10)+last_two;
while(temp!=0)
{
temp=temp/10;
digit++;
}
cout<<"You have entered the number ";
int num;
{
switch(num)
{
case 0: 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;
}
return;
}
can someone help? thanks
-
It looks like you forgot to put your code in your main function, because there is no main function.
-
-
Err, you have a lot of unesesary(sp?) code
1) You read a number for the input. If it is greater than 99 then you exit. Fine. You don't need ANYTHING else. You are done with the input. It is guaranteed to be a two digit number.
2) Since you have that number use it in the switch statement to switch. You will need 100 cases though! But that is all. One variable, one input, one if(number >99) and one switch and you are done.
3) Of course the above is too simple. So you will probably want to check the 1st digit. If it is less than 2 then have the case you have. If it is greater then have another switch. There you print "twenty", "thirty" etc etc. Then you go back the first swith and print the 2nd digit. And you are done.
Create two functions if you want. One that returns a string from 0 to 19, the other prints the first digit (twenty, forty etc). So you would have:
Code:
if (greater than 20)
cout << "number is:" << digit1(number / 10) << digit2(number %10) << endl
if (less than 20)
cout << "number is:" << digit2(number) endl
That will leave one problem. If the number is greater than 20 and the 2nd digit is 0, since it will print like "thirty zero". So use one more if (number %10 != 0) in the first if and you can guess the rest