Thread: a program that will make the number i input to words

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    a program that will make the number i input to words

    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..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So show us your best effort to date.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    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)

    Code:
    int x = 9999;
    (int)(x / 1000) is going to give u 9. there you have the thousand's place.
    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)
    Code:
    x = x % 1000; //x will be 999
    then (int)(999 / 100) gives u 9. hundreds place. a switch statement that does the same with 9 and concats hundred to the string....etc

    Code:
    x = x % 100; //x will be 99
    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.

    don't forget
    Code:
    x = x % 10; //x will be 9
    pass x through switch statement again. the one's place will be either "one", "two",...."eight", "nine".

    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
    Code:
    6000 % 100 //WRONG: returns 60 and definitely not what we want here
    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.
    Last edited by codeprada; 02-14-2011 at 11:51 AM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    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?

  5. #5
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by bobknows View Post
    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;
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM