Well I have started writing a code to create this program that i have to have done by tommorow but i am stuck at the moment. Anybody please help!!

Here is the problem:

Write a prgram that converts numbers to words. For examlpe, 834 should result in "eight three four." Likewise, 1697 should convert to "one six seven nine." You may assume that the input is less than one million. (Hint: remember the % operator!).

Is there anybody that can help me out with this?? Thanks!

6
Contributors
7
Replies
8
Views
11 Years
Discussion Span
Last Post by ~s.o.s~

You can use % 10 to find the units digit of a number. For example, n % 10 gives the units digit of n. You can then use / 10 to divide an integer by ten. For example, n / 10 divides n by ten.

So, 1697 % 10 simplifies to 7, and 1697 / 10 simplifies to 169. You can do this in a loop to fill a list or vector or array with the digits of your number, and then use a lookup table (an array with strings "zero", "one", ..., "nine") to get a string form of the number.

So what type of code would I have to write to make this work?

we r not gonna do ya home work !
just start thinking about the logic of how the program works and write ya pseudocode and zn write ya code...
and after that we will Help ya !
cheers

So what type of code would I have to write to make this work?

C or C++ might be a start. I can't believe this is your first program in the class so you should be able to do at least part of it. Then when you post something that shows an honest effort, we can help you further.

Plus we don't kow what you have learned so don't know whether vectors or arrays are useful to you. So you need to give us a little info.

I have came up with this code but everything prints out backwards. EX. I input 123 and it will output three, two, one. How do I fix this part?

Here is what I have so far:

``````#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int number, input;
scanf("%d", &input);
while ((input < 0.0) || (input > 1000000.0))
{
printf("\nError! Enat a number between 0 and 1,000,000! Try again: ");
scanf("%d", &input);
printf("\n");
}
while((input % 10) != 0) {
number=input%10;
input=input/10;
switch(number) {
case 1: puts("one"); break;
case 2: puts("two"); break;
case 3: puts("three"); break;
case 4: puts("four"); break;
case 5: puts("five"); break;
case 6: puts("six"); break;
case 7: puts("seven"); break;
case 8: puts("eight"); break;
case 9: puts("nine"); break;
default: puts("Invalid Input"); break;
}
}
getchar();
getchar();
return(0);
}``````

Let's just look through your code and figure out why it give a backwards result:

``````while((input % 10) != 0) {
number=input%10;
input=input/10;
switch(number) {
// cases...
}``````

Each iteration of the loop takes the least significant digit and reads that one to you. What you're wanting is to take the most significant digit and read that one instead.

That said, your condition for the while loop is flawed. If I were to put in 2050 as the number, the loop would never execute (2050%10 == 0). You could just change the condition to `while(input != 0)` or simply `while(input)`

In order to print backwards you need to store the variable "number" in some kind of array or linked list so that it can be printed backwards which will give you the required representation.

Why not try something more classy rather than using "switch" stmts;

``````int main()
{
int digit = 0, count = 0, tmp = 0 ;
long number = 0 ;
char buffer [BUFSIZ] = { '\0' } ;
const char* const mappings [] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" } ;

printf ( "Enter the number: " ) ;
fgets ( buffer, BUFSIZ, stdin ) ;

int* my_digits = (int*) malloc ( sizeof (int) * strlen (buffer) ) ;
number = atol ( buffer ) ;

while ( number != 0 )
{
digit = number % 10 ;
number /= 10 ;
my_digits [count] = digit ;
count ++ ;
}

printf ("\n\nThe representation : " ) ;
while ( count > 0 )
{
tmp = my_digits [count - 1] ;
fputs ( mappings[tmp], stdout ) ;
putchar (' ') ;
count -- ;
}

return 0 ;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.