Like Tree1Likes
  • 1 Post By Tclausex

Number to Text translation

This is a discussion on Number to Text translation within the C Programming forums, part of the General Programming Boards category; Hey! I need to make a code that translates numbers up to ten digits long into a text format. So ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    Number to Text translation

    Hey! I need to make a code that translates numbers up to ten digits long into a text format. So far I can get it to translate only numbers up to nine digits long correctly, also it won't print zeroes at the numbers end. Any help would be great! Her's my code so far:

    Code:
     
    Code:
    #include<stdio.h>
    
    
    int main () 
    {
        int number; //declares user input and other needed variables
        int    r=0; 
        int count = 0;  //declares a counter for the loop
        int digit;  //declares digit to be printed
    
    
        printf("enter a number: "); //asks for a number
        scanf("%d", &number);  //scans for a number
    
    
        while (number != 0)  //flips order of number input 
        {
            r=r*10;
            r=r+number%10;
            number=number/10;
        }
    
    
        number=r;
    
    
        printf("%d\n", number);
    
    
        while(number > 0) {
            digit = number%10; //finds next digit to print
    
    
            switch(digit) //determines what to print
            {
            case 0:
                printf("zero-");
                break;
            case 1:
                printf("one-");
                break;
            case 2:
                printf("two-");
                break;
            case 3:
                printf("three-");
                break;
            case 4:
                printf("four-");
                break;
            case 5:
                printf("five-");
                break;
            case 6:
                printf("six-");
                break;
            case 7:
                printf("seven-");
                break;
            case 8:
                printf("eight-");
                break;
            case 9:
                printf("nine-");
                break;
            }
        number=number/10;  //moves to next iteration correctly
    }
        printf("\n");
    
    
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    when the number > 0 the program runs, example 101 prints 1, leftover is 01, prints 0 left over is 1, prints on, left over is 0 so ends....example 100, prints 1, leftover is 00, while loop ends because all that is left is 0.

    going over 10 digits, it is the lilitation, BIG BIG numbers dont work too well!!!

    i would recommend if this is all your programming is doing, use a STRING, and use length for the loop count. then taking first char, then second char, then third char

    OR (and this is better IMO) use a char array, so you can then cast it back to ints!

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Find your limits.h file in the include directory and find INT_MAX and compare the value to a 10+ digit number.

    I would suggest you read the input as a string and deal with each char-digit, then your number can be quite large.
    Crossfire likes this.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Thanks, I'll try using a string instead!

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    So I tried to use a string instead. I counted the length of the string to determine the loop and I tried to then use a switch statement to print the correct word, but it doesn't work. It only ever prints the error message for the default case.

    Code:
     
    Code:
    #include<stdio.h>
    #include<string.h>  //includes string functions
    
    
    int main () 
    {
        char number[10]; //declares user input and other needed variables
        int length;
        int digit = 0;
        char character;
    
    
        printf("enter an integer number ten or fewer digits in length: "); //asks for a number
        scanf("%s", number);  //scans for a number
    
    
        length = strlen(number);  //determines length for the loop counter
    
    
        for (length; length >0; length--) 
        {
            character = number[digit]; //determine the character
    
    
    
            printf("%c\n", character); 
            
            switch (character) 
            {
            case 0:
                printf("ZERO-");
                break;
            case 1:
                printf("ONE-");
                break;
            case 2:
                printf("TWO-");
                break;
            case 3:
                printf("THREE-");
                break;
            case 4:
                printf("FOUR-");
                break;
            case 5:
                printf("FIVE-");
                break;
            case 6:
                printf("SIX-");
                break;
            case 7:
                printf("SEVEN-");
                break;
            case 8:
                printf("EIGHT-");
                break;
            case 9:
                printf("NINE-");
                break;
            default:
                printf("Incorrect input");
            }
            
    
    
            digit=digit+1;
        }
    
    
        return(0);
    }
    ​again thanks for any help you can give!

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Look up the ASCII table and see what the actual integer values are for the literal characters '0', '1', '2',...,'9' are.

    You have 2 choices:

    Change your switch cases to test against the literals '0', etc. instead of their representative integer values.
    or
    Convert the characters to their representative values with an expression like
    Code:
    character -= '0'
    The latter has the bad property of unnecessary conversion in this case. It's something you would do if you wanted to deal with the actual integer values rather than the character literals.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    I used the ASCII tables and that worked well. Thanks a bunch for your help, I learned a good amount!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing text to a number of pages
    By new_in_c++ in forum Windows Programming
    Replies: 2
    Last Post: 07-11-2011, 09:58 PM
  2. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 12:26 PM
  3. Search a text file for a number
    By willie in forum C Programming
    Replies: 8
    Last Post: 11-23-2008, 01:12 PM
  4. Replies: 5
    Last Post: 05-16-2004, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21