469,275 Members | 1,629 Online

# i'm trying to convert number into words 2
Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2. #include <string.h>
3. #include <math.h>
4. //i'm trying to convert number into word like for an example if i type
5. //99.99 then it will print ninety nine point ninety nine
6. //can anybody please help me
7.
8. //i have a parse error that i cant figure out
9.  int values[] = { 90, 80, 70, 60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
10.
11.
12.
13. static char *numbers[] = { "Ninety", "Eighty","Seventy", "Sixty", "Fifty", "Forty", "Thirty", "Twenty",
14.         "Nineteen", "Eighteen", "Seventeen", "Sixteen", "Fifteen","Fourteen", "Thirteen",
15.         "Twelve", "Eleven", "Ten", "Nine", "Eight","Seven", "Six", "Five", "Four", "Three", "Two", "One"};
16.
17.
18.
19. int main()
20. float amount = 0.0;
21.   printf("Enter a number: \n");
22.
23.   scanf("%f", &amount);
24.
25.   int lnum = (int)amount;
26.   int rnum = (int)((amount - lnum) * 100.101);
27.   int lnumber = lnum;
28.
30.   int index = 0;
31.   while(lnum > 0)
32.     {
33.       if(lnum >= values[index])
34.         {
37.       lnum -= values[index];
38.         }
39.       ++index;
40.     }
41.
42.   if(lnumber == 0 && rnum > 1)
43.     {
44.       printf(lnumber  , " %f point %f  ", ((rnum > 0)?(rnum):(0)));
45.     }
46.   if(lnumber == 0 && rnum == 0 )
47.     {
48.       printf("Zero");
49.     }
50.   if( lnumber == 0 && rnum == 1 )
51.     {
52.       printf(lnumber , " %f point %f  ", rnum);
53.     }
54.   if(lnumber > 1 && rnum > 1 )
55.     {
56.     printf(answer , " %f point %f ", ((rnum > 0)?(rnum):(0)));
57.     }
58.   if(lnumber > 1 && rnum == 1 )
59.     {
60.      printf(answer , " %f point %f  ",rnum);
61.     }
62.   if(lnumber > 1 && rnum == 0 )
63.     {
64.       printf( answer, " %f point %f  ", 0);
65.     }
66.   if(lnumber == 1 && rnum > 1 )
67.     {
68.      printf(answer,"%f point %f ",((rnum > 0)?(rnum):(0)));
69.     }
70.   if(lnumber == 1 && rnum == 1 )
71.     {
72.       printf(answer "%f point %f" rnum);
73.     }
74.
75.   return 0;
76. }
Oct 25 '07 #1
2 4612 weaknessforcats
9,208 Expert Mod 8TB
A couple of things here:
1) the integer operators like ==, <=, >, < ,etc don't work with floating point. Due to the automatic rounding of floating point numbers you will get an expression reporting true whenthe numbers are only close in value.

To compare two floating point numbers, you have to establish a sigma error tolerance.

Expand|Select|Wrap|Line Numbers
1. if (fabs(i - j) < 0.000001) { ... // almost equal }
2.
Google floating point arithmetic and you will be buried in info.

2) The casts arenn;t doing what you think.

I recommend you enter the data into a string and then parse the string so you can see each character. That way you can locate the decimal point and back up to see how to name the most significant digit.
Oct 25 '07 #2
mattmao
121 100+
#include <stdio.h>
#include <string.h>
#include <math.h>
//i'm trying to convert number into word like for an example if i type
//99.99 then it will print ninety nine point ninety nine

//i have a parse error that i cant figure out
int values[] = { 90, 80, 70, 60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

static char *numbers[] = { "Ninety", "Eighty","Seventy", "Sixty", "Fifty", "Forty", "Thirty", "Twenty",
"Nineteen", "Eighteen", "Seventeen", "Sixteen", "Fifteen","Fourteen", "Thirteen",
"Twelve", "Eleven", "Ten", "Nine", "Eight","Seven", "Six", "Five", "Four", "Three", "Two", "One"};

int main()
{
float amount = 0.0;
printf("Enter a number: \n");

scanf("%f", &amount);

int lnum = (int)amount;
int rnum = (int)((amount - lnum) * 100.101);
int lnumber = lnum;

int index = 0;
while(lnum > 0)
{
if(lnum >= values[index])
{
lnum -= values[index];
}
++index;
}

if(lnumber == 0 && rnum > 1)
{
printf(lnumber , " %f point %f ", ((rnum > 0)?(rnum):(0)));
}
if(lnumber == 0 && rnum == 0 )
{
printf("Zero");
}
if( lnumber == 0 && rnum == 1 )
{
printf(lnumber , " %f point %f ", rnum);
}
if(lnumber > 1 && rnum > 1 )
{
printf(answer , " %f point %f ", ((rnum > 0)?(rnum):(0)));
}
if(lnumber > 1 && rnum == 1 )
{
printf(answer , " %f point %f ",rnum);
}
if(lnumber > 1 && rnum == 0 )
{
printf( answer, " %f point %f ", 0);
}
if(lnumber == 1 && rnum > 1 )
{
printf(answer,"%f point %f ",((rnum > 0)?(rnum):(0)));
}
if(lnumber == 1 && rnum == 1 )
{
}

return 0;
}
Hi these are the error messages:
Expand|Select|Wrap|Line Numbers
1. bash-3.2\$ gcc test.c
2. test.c: In function main:
3. test.c:36: error: invalid operands to binary +
4. test.c:37: error: invalid operands to binary +
5. test.c:45: warning: passing argument 1 of printf makes pointer from integer without a cast
6. test.c:53: warning: passing argument 1 of printf makes pointer from integer without a cast
7. test.c:73: error: expected ) before string constant
In the 36th line of your code:
Because answer is a pointer to char, numbers[index] is pointer to static char, you cannot issue the + operation.

I guess you could use:
Expand|Select|Wrap|Line Numbers