user3283315
user3283315

Reputation: 5

Numbers into words issue C

I am trying to write a program that will take an integer input, and then convert it to words. for example: 123, one two three. Also -3908, negative three nine zero eight. My code works 90% of the time, the only issue coming along when i am putting one or more zeros on the end of the integer. eg. 70800 will come up as seven zero eight. It completely misses the end zeros. I understand why that is happening but does anybody know if there is a way around it. PS(i am not allowed as a part of this task to accept the input as a string and split it into an array, so it would be best if the answer is based off this code).

int main(int argc, const char * argv[])
{

    @autoreleasepool {



        float abNumber;
        int i = 0;
        float number;
        float result;
        float firstNumber;

        printf("type a number: ");
        scanf("%f", &firstNumber);

        abNumber = abs(firstNumber);

        if (firstNumber < 0) {
            printf("negative ");
        }

        number = abNumber;

        while (number >= 10) {
            number = number / 10;
            i++;
        }

        do {
            float countNumber = abNumber;
            float power = powf(10, -i);
            float powerNo2 = powf(10, i);

            countNumber = countNumber * power;
            result = floorf(countNumber);

            if (result == 9){
                printf("nine ");
            }
            if (result == 8){
                printf("eight ");
            }
            if (result == 7){
                printf("seven ");
            }

            if (result == 6){
                printf("six ");
            }

            if (result == 5){
                printf("five ");
            }

            if (result == 4){
                printf("four ");
            }

            if (result == 3){
                printf("three ");
            }

            if (result == 2){
                printf("two ");
            }

            if (result == 1){
                printf("one ");
            }

            if (result == 0){
               printf("zero ");
            }
            while (abNumber > powerNo2) {
            abNumber = abNumber - powerNo2;
            }



            i--;

        } while (i >= 0);





    }
    return 0;
} 

Upvotes: 0

Views: 108

Answers (4)

Fiddling Bits
Fiddling Bits

Reputation: 8861

Consider the following:

Code

#include <stdio.h>
#include <stdlib.h>

const char *numbers[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void printNum(int num);

int main(void)
{
    int num;

    printf("Enter a number: ");
    scanf("%u", &num);

    printNum(num);
    printf("\n");

    return 0;
}

void printNum(int num)
{
    int absNum = abs(num);

    if(absNum > 9)
        printNum(num / 10);
    if((absNum < 10) && (num < 0))
        printf("negative");
    printf(" %s", numbers[absNum % 10]);
}

Example Output

Enter a number: 2582  
 two five eight two

Enter a number: -943
negative nine four three

Enter a number: 1000
 one zero zero zero

Enter a number: -1000
negative one zero zero zero

Logic

  1. Get an integer from user.
  2. Send to recursive function.
  3. Keep recursing until the least significant digit is left. This is to print in the correct order.
  4. Print digit as a string by using a lookup table.
  5. Keep someone a high five.

To Do

  1. Error checking

Upvotes: 0

Dabo
Dabo

Reputation: 2373

I would go for recursive solution, like that

int print(int num)
{
    if( num )
    {
        int mod = num%10;
        print(num/10);
        switch(mod)
        {
        case 0:printf(" zero");break;
        case 1:printf(" one");break;
        case 2:printf(" two");break;
        case 3:printf(" three");break;
        }

    }
    return 0;
}

Recursivy divide the number untill nothing left of it, on the way back print the mod.

Upvotes: 1

rullof
rullof

Reputation: 7424

Why don't you just input the number as a string then loop through each character:

Exemple: http://ideone.com/E8QspN

Input:

-12003200

Output:

negative one two zero zero tree two zero zero

Code:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char input[25];
    scanf("%s", input);

    int i = 0;

    while (input[i] != '\0') {
        switch(input[i]) {
            case '-' :
                printf("negative");
                break;
            case '0' :
                printf("zero");
                break;
            case '1' :
                printf("one");
                break;
            case '2' :
                printf("two");
                break;
            case '3' :
                printf("tree");
                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 :
                break;
        }
        printf(" ");
        i++;
    }

    return 0;
}

Upvotes: 0

Martin R
Martin R

Reputation: 539725

The main error seems to be that

while (abNumber > powerNo2) {

should be

while (abNumber >= powerNo2) {

But I would recommend not to use floating point arithmetic at all, to avoid possible rounding errors. The same can be achieved with simple integer arithmetic (I have omitted the "negative case" for simplicity):

int number;
printf("type a number: ");
scanf("%d", &number);

// Determine highest power of 10 that is <= the given number:
int power = 1;
while (10 * power <= number) {
    power *= 10;
}

// Extract each digit: 
while (power > 0) {
    int digit = (number / power) % 10;
    /*
     * Use switch/case to print 'digit' as a string ...
     */
    power /= 10;
}

Upvotes: 1

Related Questions