# Thread: converting numbers to letters

1. ## converting numbers to letters

ive made this program, and am getting an error, plz help!
im wanting the user to enter in integers, and the program will convert the integers into words.

Code:
```#include <stdio.h>

int PrintDigit(int number);

int main(void)
{

int inputNumber;
int rc;
int remainder;
int divisor;
int digit;

rc = scanf("&#37;d",&inputNumber);

if(inputNumber==0)

printf("ZERO\n");

else if(inputNumber < 0)
{
printf("Minus");
inputNumber = inputNumber*-1;
printf("%d", inputNumber);

remainder = inputNumber;
divisor = 10000;

while(divisor>0)
{
if(inputNumber>=divisor)
{
digit = (remainder/divisor);
remainder = remainder % divisor;
PrintDigit(digit); // Passing digit to function PrintDigit
}
divisor = divisor/10;
}
}

return(0);
}

int PrintDigit(int number)
{

int digit;

switch(digit)
{
case 0:	printf("zero");
break;
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("unknown digit detected");
return(number);
}
}```

2. And.... the error is?

3. sorry.... more of a problem then an error...

im getting stuck inside of my else if(inputNumber < 0) loop

i cant really figure out why the program is not working... :S

help plz?

4. if inputNumber > 0, is it meant to not do anything?

5. Well... ok, let's start with a few problems in the code:

1. Your first printf() is wrong, since it doesn't need the second argument.
2. Your program only handles numbers less than or equal to 0.
3. Your switch() statement in PrintDigit() uses a local variable that has not been initialized instead of using the int that is passed to it. To put it mildly, this is silly.

6. Code:
```#include <stdio.h>

int PrintDigit(int number);

int main(void)
{
int inputNumber;
int rc;
int remainder;
int divisor;
int digit;

/* You don need to send the address of variable*/
/* You get the  retrun value but wont do anything with it.*/
scanf("&#37;d",&inputNumber);

if(inputNumber==0)
printf("ZERO\n");
/* So the program works only for the negative values, but not for positive
which mean it wont print what u wanted */
else if(inputNumber < 0)
{
/* So u are trying to convert the negative to postive values here.
it would be more better if you give a error message for a negative values
and the postive values from the user */
printf("Minus");
inputNumber = inputNumber*-1;
printf("%d", inputNumber);

remainder = inputNumber;
divisor = 10000;

while(divisor>0)
{
if(inputNumber>=divisor)
{
digit = (remainder/divisor);
remainder = remainder % divisor;
PrintDigit(digit); // Passing digit to function PrintDigit
}
divisor = divisor/10;
}
}
return(0);
}

int PrintDigit(int number)
{
int digit;
/* should you be sending the number as an argument to your switch ?? */
switch(digit)
{
case 0: printf("zero");
break;
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("unknown digit detected");
return(number);
}
}```
You need a proper code indentation. Apart i dont see any were in your code. where you try to convert the actual int to char. That was the main aim of the problem wasn't it.

There are some commnets along the code. Go through it.

NOTE: lol even I tried indenting your code. It dint work. Editor miss match.
ssharish2005

7. okay... thank you for all of the feedback.

i modified the code to work properly now.

Code:
```#include <stdio.h>
#define neg -1

//Function prototype section
int PrintDigit(int number);

int main(void)
{

int inputNumber;

int rc;
int remainder;
int divisor;
int digit;

rc = scanf("%d",&inputNumber);

if(inputNumber==0)

printf("ZERO\n");

else if(inputNumber < 0)
{
printf("Minus");
inputNumber = inputNumber*neg;   // Making the input number positive

remainder = inputNumber;
divisor = 10000;

while(divisor!=0)
{
if(inputNumber>=divisor)
{
digit = (remainder/divisor);
remainder = remainder % divisor;
PrintDigit(digit); // Passing digit to function PrintDigit
}
divisor = divisor/10;
}
}

else if(inputNumber > 0)
{

remainder = inputNumber;
divisor = 10000;

while(divisor!=0)
{
if(inputNumber>=divisor)
{
digit = (remainder/divisor);  // Declaring digit = inputNumber / divisor
remainder = remainder % divisor; // Checking for remainders
PrintDigit(digit); // Passing digit to function PrintDigit
}
divisor = divisor/10;
}
}

printf("\n\n\n");
return(0);
}

/****************************************************************************************
//
//  FUNCTION:  Function PrintDigit
//
//  WHAT IT DOES:  This function is passed a number, then prints the corresponding word
//					based on the number that it receives.  There are 11 cases in total.
//					If a case is true, the cases before the true case, are bypassed, then
//					a printf is executed, a break occurs, then the number is returned.
//
//***************************************************************************************/

int PrintDigit(int number)
{

//int digit;

switch(number)
{
case 0:	printf(" zero");
break;
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(" unknown digit detected");
return(number);
}
}```