Nipponshin
Nipponshin

Reputation: 23

Program that displays each digit on an integer in english doesn't work with an integer beginning with "0"

I have an assignment were I have to write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. I'm not able to use arrays or recursion, we're just starting with programming.

For example: "123" returns "one two three"

My program is working well (for the most part), but the problem is that when you enter something like "0123" in the terminal the program returns "eight three"... WTH??

This is my code:

// Program that takes an integer and displays each digit in English

#include <stdio.h>

int main (void)
{
    int num, digit;
    int reversed = 0, backupZero = 0;

    printf("Please enter an integer:\n");
    scanf("%i", &num);

    if (num == 0) // In case the input is just "0"
    {
        printf("zero");
    }

    while (num > 0) // Loop to reverse the integer
    {
        digit = num % 10;
        reversed = (reversed * 10) + digit;

        if ((reversed == 0) && (digit == 0)) // If the integer finishes in zero
        {
            ++backupZero; // Use this to add extra zeroes later
        }

        num /= 10;
    }

    while (reversed > 0)
    {
        digit = reversed % 10;
        reversed /= 10;

        switch (digit)
        {
            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("zero ");
                break;
        }

    }

    for (int counter = 0; counter < backupZero; ++counter) // Prints the extra zeroes at the end
    {
        printf("zero ");
        --backupZero;
    }

    printf("\n");

    return 0;
}

Probably is something on the mathematics, I admit I'm not good at it.

Upvotes: 1

Views: 6575

Answers (4)

There are some mistakes:

if ((reversed == 0) && (digit == 0)) (incorrect)
if ((reversed == 0) || (digit == 0)) (correct)

And in the last loop you should remove

--backupZero;

And code will read numbers better

Upvotes: 0

Dmytro Karataiev
Dmytro Karataiev

Reputation: 1304

Oh, wow. It took me 3 or 4 hours to write following code. I'm into only first week, so please be considerate. Update: added working minus + some comments.

#include <stdio.h>
#include <math.h>

int main(void)

{
    int num, count, user, out;
    count = 0;

    printf("Type in any int: ");
    scanf("%d", &num);

    // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }
    user = num;

    // creating a power to the future number
    while (num != 0)
    {
       num = num / 10;
       count++;
    }

    int i2;
    i2 = count;

    // main calculations: dividing by (10 to the power of counter) and subtracting from the initial number
    for (int i = 0; i < i2; i++)
    {
        out = user / pow(10, count - 1);
        user = user - out * pow(10, count - 1);
        count--;

        switch (out)
        {
            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;
            case 0:
                printf("zero ");
                break;
            default:
                break;
         }
    }
    printf("\n");

    return 0;
}

Upvotes: 0

barak manos
barak manos

Reputation: 30136

If you want to handle numbers that start with '0', then I suggest that you read the user input as a string (array of characters) rather than as an integer.

In addition to that, instead of "doing a switch" on each character, you can use a simple array in order to map the correct word to each digit.

Here is one way for implementing it:

#include <stdio.h>

#define MAX_INPUT_LEN 100

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

int main()
{
    int i;
    char format[10];
    char str[MAX_INPUT_LEN+1];
    sprintf(format,"%c%us",'%',MAX_INPUT_LEN); // now format = "%100s"
    scanf(format,str); // will write into str at most 100 characters
    for (i=0; str[i]!=0; i++)
    {
        if ('0' <= str[i] && str[i] <= '9')
            printf("%s ",digits[str[i]-'0']);
        else
            printf("invalid character ");
    }
    return 0;
}

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227390

When you read in the number with

scanf("%i", &num);

You are letting scanf infer the base of the number. Numbers starting with 0 followed by other digits are interpreted as octal. So 0123 is not the same as 123. It is in fact, 83.

0100 = 64
 020 = 16
  03 =  3
---------
0123 = 83

To read the number as base 10, use

scanf("%d", &num);

Upvotes: 3

Related Questions