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!