I have written this program which changes the entered height (in cm) to feet and inches. When I run it, the result keeps coming up without stopping. Does anyone know why?
#include <stdio.h>
int main (void)
{
float heightcm;
float feet;
float inch;
printf("Enter height in centimeters to convert \n");
scanf("%f", &heightcm);
while (heightcm > 0)
{
feet = heightcm*0.033;
inch = heightcm*0.394;
printf("\n %0.1f cm = %0.2f feet and %0.2f inches \n", heightcm,feet,inch);
}
return 0;
}
You made an infinite loop:
No where in the loop is
heightcmchanged, which means that it’s always> 0and your function will loop forever and never terminate. Aifcheck makes more sense here:Or you can use your while loop and keep asking for more input:
Which is probably what you were going for (loop until the user enters a non-positive number)