Please read this whole post before commenting. I am new to programming in C. The problem I have is from Kochan's "Programming in C," Chapter 6, Exercise 6. The problem states that I need to take user input (some integer) and display it in english. Ex. User inputs "123", output should be "one two three." This next part is important: I can NOT use arrays. At this point in the book I do NOT know how to use arrays. Sorry if that comes across as snarky, the last two forums I posted on insisted I use them. I will, believe me, but not right now. Here is the code I have so far to display input greater than 9 in english:

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 will use switch statements later, for now please forgive my use of "if." int temp = int number. int number is the user input. int counter and int digit = 0 at declaration. int ten = 1 at declaration.

The output I am getting currently, no matter what the input is, is the first two digits displayed in english and nothing else. What is wrong with the logic in my code?

SPECIAL NOTES: I know the code is not pretty. I repeat, I cannot use arrays and I am new to programming in C, I have been learning it for 7 hours now. I am not assuming anyone here is mean to newbies, but on the last forum I posted on, the only reply was from someone who wanted to tell me my code was garbage and not even worth reviewing. If you feel the need to do this, please email me at frohanyo@hotmail.com, and do not post here. Thanks for any help in advance!

Give us a sample input and the output you get.

We don't know what value temp begins with. We don't know what kind of objects the variables are. We need the code that goes around this.

Edited 3 Years Ago by Moschops

Sample input: 142547
Output: one four
Sample input_2: 873
Output: eight seven
Sameple input_3: 6357357012415
Output: six three

Here is the code that you need, this goes at the beginning of the program:

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

int main(void)

{
    int number, digit, temp, counter, i, ten, x;

    digit = 0;
    counter = 0;
    ten = 1;

    printf ("Enter a number: ");
    scanf ("%i", &number);
    temp = number;

Put this in:

printf ("digit = %d \n", digit);

right after line 42 in the top section of code. You'll see what's going on.

This article has been dead for over six months. Start a new discussion instead.