if else switch nt working

the programn is like this



#include<stdio.h>
#include<math.h>
#define PI 3.1416
#define g 9.81
void main(void)
{
/*initialisation*/
int u,alpha,MAXHEIGHT;
/*input*/
printf("Enter the initial velocity and the angle (in degree):");
scanf("%f%f",&u,&alpha);
/*calculation*/
MAXHEIGHT=(u*u*(pow(sin(alpha*PI/180.0),2))/(2*g));
/*output*/
if (alpha>=0 && alpha<=180)
{
printf("The maximun height is %.4ld\n",MAXHEIGHT);
}

else
{
printf("input angle should lie between 0 and 180\n");
} // end else
} // end main



for some reason,when i type in alpha as any number,the else statement comes out.can anyone plz help me?new to C programing,thxs.
This is a C++ forum, not a C forum. Please use code tags.
Please check the scanf documentation on this site. I doubt %f is correct to read an int value.
%f is a float, so that should work for an int, but I wouldn't recommend it. What exactly do you mean by "the else statement comes out".
If %f is for float, then it will definitely NOT work for an int. Their binary layouts obviously are nothing alike.
Their "binary layouts" are identical.

Float:
0000 0000 0000 0000 0000 0000 0000 0000
Int:
0000 0000 0000 0000 0000 0000 0000 0000

Floats just have the ability to hold decimals.

You should still be using float to hold the numbers anyway.
@Vexer
There are more numbers than 0, you know.
+1 for Athar. %f does not work for ints.

Also scanf() with "%f%f" wouldn't work. Say you have the following string:

"123456"

what would that put in u and alpha?

Would it be u=123 and alpha=456? or u=12 and alpha=3456? or u=123456 and alpha=?
1stly,thxs to all of you for replying.i am new n dun understand the rules so plz try to tolerate with me.i tried replacing the int with float.the calculation went wrng,and when i types the else statement conditions,noting comes out.And this is a C++,i think,cause i chose it on the select screen,and it says C++.And what is code tags?
here is the program


#include<stdio.h>
#include<math.h>
#define PI 3.1416
#define g 9.81
void main(void)
{
/*initialisation*/
float u,alpha,MAXHEIGHT;
/*input*/
printf("Enter the initial velocity and the angle (in degree):");
scanf("%f%f",&u,&alpha);
/*calculation*/
MAXHEIGHT=(u*u*(pow(sin(alpha*PI/180.0),2))/(2*g));
/*output*/
if (alpha>=0 && alpha<=180)
{
printf("The maximun height is %.4ld\n",MAXHEIGHT);
}

else if(alpha<0 && alpha>180)
{
printf("input angle should lie between 0 and 180\n");
} // end else
} // end main
@Vexer
There are more numbers than 0, you know.


Yes, I was showing the numbers in binary.
Yes, I was showing the numbers in binary.

I'm aware. I was referring to the fact that 0 is probably the only number where the int representation and the float representation are equal.
Topic archived. No new replies allowed.