Reputation: 29
I'm absolutely new to C language and computer science. I'm still learning how to code. This down below is a little program I wrote which converts the height in cm to feet and inches, however, the terminal crashes after it takes the height_cm
as an input. I thought for a long time but cannot figure out the reason behind it.
#include <stdio.h>
int main(void)
{
float height_cm;
float feet=(int)(height_cm/2.54)/12;
float inch=(height_cm/2.54);
while(height_cm > 0)
{
printf("Enter a height in centimeters(<=0 to quit): ");
scanf("%.2f", &height_cm);
printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
}
printf("Bye");
return 0;
}
Upvotes: 0
Views: 111
Reputation: 1213
Your main problem: You are referencing a unassigned variable height_cm
when you are calculating feet
and inches
. This is going to produced a undefined behavior because the value in that variable is a junk value. The below snipped of code addresses some of you other problems, such as using %.2f
in scanf
, and performs your desired logic.
#include <stdio.h>
int main(void)
{
float height_cm; // Currently junk value
int feet; // Currently junk value
float inch; // Currently junk value
// Keep calculating feet / inches as long as the entered hight is positive
do {
printf("Enter a height in centimeters(<=0 to quit): ");
scanf("%f", &height_cm); // Can only use "%.2f for printf not scanf"
feet=(int)(height_cm/2.54)/12;
inch=(height_cm/2.54);
// Output results
printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
}
while (height_cm > 0);
printf("Bye");
return 0;
}
Upvotes: 6
Reputation: 2116
As others have stated, you are checking height_cm
before assigning it a value. You can make a simple change to execute the loop at least once prior to checking for 0
by replacing the while
with a do ... while
loop:
#include <stdio.h>
int main(void)
{
float height_cm;
float feet=(int)(height_cm/2.54)/12;
float inch=(height_cm/2.54);
do
{
printf("Enter a height in centimeters(<=0 to quit): ");
scanf("%.2f", &height_cm);
printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
} while(height_cm > 0);
printf("Bye");
return 0;
}
Upvotes: 0
Reputation: 211550
You're specifying %d
for your feet
variable which is quite clearly a float
, though oddly it's a float
based on a rounded value, so you could use int
if you wanted.
Turn on warnings like -Wall
to be alerted to simple mistakes like this.
You'll also want to check for uninitialized variables as these are the primary source of crashes.
You can't use while
on a variable that isn't defined. You must define it first.
Consider restructuring:
float height_cm;
while (true)
{
printf("Enter a height in centimeters(<=0 to quit): ");
scanf("%.2f", &height_cm);
if (height_cm > 0) {
int feet = (height_cm/2.54)/12;
float inch = (height_cm/2.54) % 12; // Don't forget to modulo
printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
}
else {
break;
}
}
Due to how you're asking for input and looping around it you'll want to conditionally break
rather than express the break condition in the while
itself.
Upvotes: 0