Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having trouble coding a problem from Kochan's "Programming in C," Chapter 6, Exercise 6. The problem is to take user input (an integer) and display it in english. Ex. User inputs "123" output should be "one two three." At this point in the book, I should only have basic knowledge of C (no arrays, no fancy functions) so I cannot use suggestions that include anything other than the basics. Here is what I have so far to display integers greater than 9 in english:

C#
if (number > 9) {
            while (temp >= 1 ) {
                temp = temp / 10;
                ++counter;
            }
            for (i = 1; i <= (counter - 1); i++) {
                digit = number / pow(10, (counter - i));
                if (digit == 9) {
                    printf ("nine ");
                }
                else if (digit == 8) {
                    printf ("eight ");
                }
                else if (digit == 7) {
                    printf ("seven ");
                }
                else if (digit == 6) {
                    printf ("six ");
                }
                else if (digit == 5) {
                    printf ("five ");
                }
                else if (digit == 4) {
                    printf ("four ");
                }
                else if (digit == 3) {
                    printf ("three ");
                }
                else if (digit == 2) {
                    printf ("two ");
                }
                else if (digit == 1) {
                    printf ("one ");
                }
                x = 1;
                for (x = 1; x <= counter - i; x++) {
                    ten = ten * 10;
                }
                    number = number % ten;
        }
        }
                digit = number;
                if (digit > 0 && digit < 10) {
                if (digit == 9) {
                    printf ("nine ");
                }
                else if (digit == 8) {
                    printf ("eight ");
                }
                else if (digit == 7) {
                    printf ("seven ");
                }
                else if (digit == 6) {
                    printf ("six ");
                }
                else if (digit == 5) {
                    printf ("five ");
                }
                else if (digit == 4) {
                    printf ("four ");
                }
                else if (digit == 3) {
                    printf ("three ");
                }
                else if (digit == 2) {
                    printf ("two ");
                }
                else if (digit == 1) {
                    printf ("one ");
                }
        }


NOTES: I know I am not using switch statements, I will change that later. int temp is equal to int number. int number is the value of the user input. int digit and int counter = 0 at declaration. int ten = 1 at declaration.

The output I am getting right now, no matter how long the inputted integer is, is the first two digits displayed in english. What is wrong with the logic in my code?
Posted
Comments
Sergey Alexandrovich Kryukov 10-May-13 12:37pm    
Quite a poor attempt. At least removed this long "if" block, use an array of strings...
—SA
Drgy55 10-May-13 12:48pm    
Hey. Sergey. I'm new to programming. I am not good at it yet. Deal with it. Also, I specifically said, "no arrays" at the top of this post. The book expects me to solve this problem using loops, if statements, and variables. That's it. Now stop being critical of those who aren't as good as you are, O mighty lord, and don't post anything if you're not going to help. Thank you.
Sergey Alexandrovich Kryukov 10-May-13 13:09pm    
You are rude.

Do you think that you are more free then others. If you thing that you are free to post what you are posting (even though your words tell me that this is the attempt to cheat at your school), why can you tell me what I can post and what not? Freedom is freedom.

And it's really dishonest to tell me that I'm not helping when I practically gave you a complete solution. If you think that a solution is a complete code, you are not learning programming. And no one can make you learning except yourself. If you are going to behave like that, nobody will help you.

—SA
Drgy55 10-May-13 12:39pm    
...I'm sorry? I'm new to programming in C friend. And I don't know how to use arrays at this point in the book. I'm doing what I can with what I know. If you don't want to help at least don't be rude about it.
Sergey Alexandrovich Kryukov 10-May-13 12:47pm    
If you don't know that, stop what you are doing and learn it. If you are a beginner, do begin, instead of complaining and playing the offended card. Let me tell you: this is simply dishonest. I'm trying to help your to learn something, but you are saying me such things. Please, check yourself, don't think that your behavior will be welcome on this site. Imaging that you tell such things to your professor, what response will you expect? You with your professor you are not so brave?
—SA

This code is so naive that it does not worth reviewing. This is not even programming.

This is the first step you could do: put all your English numeral in some array of strings. Than, if you have a number 0..9, its English representation of it will be found in one line of code; this is the element of the array indexed by that number.

On top of that, people easily developed units "translating" multi-positional numbers into combination of English words, such as "five hundreds thirty two", controlling plural/singular switching in all the cases ("hundreds" or "thousands" vs. "hundred" or "thousand", and so on). Even though it would require more considerable effort, this is still easy enough, as English language is really, really simple, in this respect.

—SA
 
Share this answer
 
v2
I fixed some bugs (uninitialized variables). Please note: you should handle the digit 0 (zero) too.

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

void show_number(int number)
{
    int counter = 0;
    int i, temp, digit, x, ten;
    if (number > 9) {
        temp = number;
        while (temp >= 1 ) {
            temp = temp / 10;
            ++counter;
        }
        for (i = 1; i <= (counter - 1); i++) {
            digit = (int)number / pow(10.0, (counter - i));
            if (digit == 9) {
                printf ("nine ");
            }
            else if (digit == 8) {
                printf ("eight ");
            }
            else if (digit == 7) {
                printf ("seven ");
            }
            else if (digit == 6) {
                printf ("six ");
            }
            else if (digit == 5) {
                printf ("five ");
            }
            else if (digit == 4) {
                printf ("four ");
            }
            else if (digit == 3) {
                printf ("three ");
            }
            else if (digit == 2) {
                printf ("two ");
            }
            else if (digit == 1) {
                printf ("one ");
            }
            x = 1;
            ten =1;
            for (x = 1; x <= counter - i; x++) {
                ten = ten * 10;
            }
            number = number % ten;
        }
    }
    digit = number;
    if (digit > 0 && digit < 10) {
        if (digit == 9) {
            printf ("nine ");
        }
        else if (digit == 8) {
            printf ("eight ");
        }
        else if (digit == 7) {
            printf ("seven ");
        }
        else if (digit == 6) {
            printf ("six ");
        }
        else if (digit == 5) {
            printf ("five ");
        }
        else if (digit == 4) {
            printf ("four ");
        }
        else if (digit == 3) {
            printf ("three ");
        }
        else if (digit == 2) {
            printf ("two ");
        }
        else if (digit == 1) {
            printf ("one ");
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900