can you help me solve this problem..
how can i make a program that will make the number i input to words..
numbers from 0-9999
using only the case..
the only thing i know is the numbers up to 0-9.. please help me..
Printable View
can you help me solve this problem..
how can i make a program that will make the number i input to words..
numbers from 0-9999
using only the case..
the only thing i know is the numbers up to 0-9.. please help me..
So show us your best effort to date.
i can give a quick run down of what your code should look like.
every number follows a particular pattern.
1000 = one thousand
2000 = two thousand
555 = five hundred and fifty five
556 = five hundred and fifty six
810 = eight hundred and ten
820 = eight hundred and twenty
this is the part where u have to use your brain. finding out what pattern the number applies to.
you must test each number like this (since 9999 is the highest number you want to go to i'll use that as my high range)
(int)(x / 1000) is going to give u 9. there you have the thousand's place.Code:int x = 9999;
have a switch statement that'll change the 9 to nine and concat thousand on it.
this small piece of code will ensure that x has 1 less place (in this case the thousands place)
then (int)(999 / 100) gives u 9. hundreds place. a switch statement that does the same with 9 and concats hundred to the string....etcCode:x = x % 1000; //x will be 999
from 99 downwards it's gonna be a little different. instead of 9 hundred and 9 thousand you must concat ninety (or eighty or seventy...you get the point ) to the string.Code:x = x % 100; //x will be 99
don't forget
pass x through switch statement again. the one's place will be either "one", "two",...."eight", "nine".Code:x = x % 10; //x will be 9
to test for numbers like 100, 5000....etc will return 0 when u do the MOD % operation on them.
Code:6000 % 1000 //returns 0
Code:500 % 100 //returns 0
hope u understand the very simple logic i'm trying to show you since we know 9999 will be the highest number and btw nested switch statements is the way to go.Code:6000 % 100 //WRONG: returns 60 and definitely not what we want here
do you have to set the first 2 digits as one string or is there a way to make it say thirteen, fourteen, fifteen.... without having to make 100 seperate strings for the first 2 digits?
other than writing it out manually which a switch statement the only other thing that comes to mind are maps
Code:#include <map>
...
...
int main()
{
map<int, string> numbers;
numbers.insert(make_pair(11,"eleven"));
numbers.insert(make_pair(12,"twelve"));
...
...
numbers.insert(make_pair(19,"nineteen"));
return 0;
}