# Thread: noob need help with programming question!

1. ## noob need help with programming question!

hey im doing this program for school and its about a user entering their height in centimeters and output it as height in feet and inches im getting the problem when getting the remainder of the feet to be changed into inches and the compiler said that i have an invalid % operand and i know it has to do with the remainder of the feet thing but i just dont know how to do it.

Code:
```/* This program converts height in centimeters and
displays it into height in centimeters, feet, inches.*/

#include<stdio.h>
const double CENT_FEET=0.033;
const double CENT_INCHES=0.30;
int main(void) {

int cent, centf, centi, centl;

printf("COnvert Height Into Feet & Inches!\n");
scanf("%d", &cent);
while (cent>0)
{
centf=(int) cent*CENT_FEET;
centi=(int) cent % CENT_FEET*CENT_INCHES;

printf("%d cm= %.2d feet, .2%d inches\n", cent, centf, centi);
printf("Enter Another Height to COntinue:    (<=0 to quit)\n");
scanf("%d", &cent);
}
printf("BYE!\n");
return 0;
}
/*----------------------------------------------------------------------*/```

2. % operator gives you the remainder
like
9%7=2
Can you find me the remainder of 5 when divided by say 1.4.
I hope you got my point

3. i understand that part right but its to get the actualy value of the remainder to be converted to inches ..i dunno if my code is wron glike fom the declaration of from the type of variable i used

4. If you include math.h in your header you can find the fractional part of cent*CENT_FEET using floor. Then using that fractional part, and knowing there are twelve inches in a foot, you can find the amount of inches that fractional part represents. And presto, you're done.

5. Code:
```/* This program converts height in centimeters and
displays it into height in centimeters, feet, inches.*/

#include<stdio.h>
#include<math.h>
const double CENT_FEET=0.033;
const double CENT_INCHES=0.30;
int main(void) {

int cent, centf, centi, centl;

printf("COnvert Height Into Feet & Inches!\n");
scanf("%d", &cent);
while (cent>0)
{
centf=(int) floor(cent*CENT_FEET);
centi=(int) (cent - floor(centf*CENT_FEET))*CENT_INCHES;

printf("%d cm= %.2d feet, .2%d inches\n", cent, centf, centi);
printf("Enter Another Height to COntinue:    (<=0 to quit)\n");
scanf("%d", &cent);
}
printf("BYE!\n");
return 0;
}
/*----------------------------------------------------------------------*/```
it looks like this now but it still wont work itsays that undefined reference to floor isnt that the right header file thoguh or did i do something wrong...

6. Do you need to link with the math library? Does your program compile? It looks good to me.

Look into a do-while loop; it would reduce the repeated code you have.

7. i dont think i need the math library but i don really know ho wto do the odo while loop yet can u help me there?

8. Okay. A do-while loop is the same as a while loop, except the condition is executed at the end of the loop, not at the start.
Code:
```printf("*");
while(x) {
printf("*");
x --;
}```
is the same as
Code:
```do {
printf("*");
x --;
} while(x);```
A do-while loop is usually used when the code has to execute at least once.

9. Your code looks good, except for the unreferenced variable centl, and the "%.2d" in printf().

And the ".2%d", too. Make them regular "%d"s.
[/edit]

10. arite thanx

11. it still says undefined reference to floor i dunno why

12. What compiler are you using?

If floor() doesn't work, a cast to int does the same thing:
Code:
```double d = 1.99;
printf("%f\n", floor(d));```
and
Code:
```double d = 1.99;
printf("%f\n", (double)((int)d));```
Output:
Code:
`1.000000`

13. thanx i got it now