Thread: Shorter way to convert number to txt

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Shorter way to convert number to txt

    guys,
    I'm currently working on this piece of work where i'm required to write a c code that will allow the user to input a 4 interger then the code works to change it to word form as in eg.
    user input>5742
    output> Five Seven Four Two

    i've managed to get the first part of the code where I need to break it down to work using
    Code:
     
      printf("Enter a number> ");
         scanf("%d", &interger);
         printf("\n\n");
         
        
         th = interger / 1000;
         
         hun = interger / 100;
         hun %= 10;
         
         ten = interger / 10;
         ten %= 10;
         
         unit = interger%10;
    then if i wanted the program to show it in words i could very much use switch but the code would be very long as i'd need to make switches for every part of the integer so i was wondering is there any way that i can do it without using arrays as i've not gotten to that part yet?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Code:
     
    <include stdio.h>
    int name(int x)
    {
    switch(x)
    {case 1:
    printf("one ");break;
    case 2:
    printf("two ");break;
    case 3:
    printf("three ");break;
    /*make it until 0*/
    }
    int main(){
      printf("Enter a number> ");
         scanf("%d", &interger);
         printf("\n\n");
         
        
         th = interger / 1000;
         hun = (interger%1000) / 100;
         ten = (hun%100)/ 10;
         unit = (ten%10);
    name(th);name(hun);name(ten);name(unit);//it would print all of them
    }
    may be like this...
    or you can put this
    th = interger / 1000;
    hun = (interger%1000) / 100;
    ten = (hun%100)/ 10;
    unit = (ten%10);

    in a function.. so you just sent your integer to that function..

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    owh sorry.. I forgot to "return 0" at the function.. hehehe..
    or you can also make the function char... so you can return char..

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    kinshara;
    thanx! tatz exactly the way i wanted to do but didnt know how to! thanx thanx thanx! and i'd be honest i don't really understand the way these functions work like where did (int x) came from i refer to the X here as in :s and how does the int x works lol

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    you welcomee.. ^_^
    I am still new to in C... if you want, we can teach each other ... ^^,
    Code:
    int name(int x)
    /* this one mean, the name of the function is "name"
    so (int x) is the parameter.. which it need some value int there..
    */
    {
    switch(x)
    {case 1:
    printf("one ");break;
    case 2:
    printf("two ");break;
    case 3:
    printf("three ");break;
    /*make it until 0*/
    }
    
    name(th);name(hun);name(ten);name(unit);//it would print all of them
    /*
    and this is how to use the function..
    just call the name and fill the parameter with the number you want sent to the function..
    (the value of th will sent to the function as variabel x, which we declare it above)
    */
    Last edited by Kinshara; 11-01-2009 at 12:47 AM.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    so ure saying basically i can use any int as long as i name int x and not some definite name? like x is the wildcard here?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    yapp... when you declare x as int in that function, it would use if you call it inside that function..
    so, in other function it was unknown variable...
    like x is only for name function(int name(int x))

    but in main function, x is unknown, until you declare it.. but the value isn't same.. and the type can be different(as you want)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. Convert a Floating Point Number to a String
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 04-21-2005, 04:40 PM
  4. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  5. Convert type string to a number
    By ejholmes in forum C# Programming
    Replies: 2
    Last Post: 04-21-2004, 01:33 AM