May 12, 2013 at 10:41pm UTC
So obviously I'm a beginner at programming and we're writing a program for one of my classes. I feel like I understand everything pretty well, but I'm stuck on a program using if/else statements. I've tried the else statement as is, and with else ( &radius <= 0 ) and neither one is working properly. Basically, what it's supposed to do is display an error message if the radius is less than or equal to 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <stdio.h>
int main()
{
float radius;
float area;
printf( "Please enter radius: " );
scanf( "%f" , &radius );
if ( &radius > 0 ){
area = radius * radius * 3.14;
printf( "The area is %f" , area );
getchar();
return 0;
}
else {
printf( "error\n" );
getchar();
return 1;
}
}
Last edited on May 12, 2013 at 10:41pm UTC
May 12, 2013 at 10:50pm UTC
This statement
if ( & radius > 0 ){
means that you are comparing the address of variable radius with zero. To compare the radius itself with 0 you should write
if ( radius > 0 ){
May 13, 2013 at 12:06am UTC
Thank you very much! That worked, but now it's getting stuck at the printf part? Not sure why.
May 13, 2013 at 12:19am UTC
What do you mean "getting stuck"?
You might want to include a \n in line 12 to flush the print buffer.
May 13, 2013 at 12:32am UTC
It stops at printf ("error\n"); like it's trying to calculate the area still, instead of displaying "error" and terminating.
May 13, 2013 at 1:02am UTC
Do you mean it stops before printing "error" or after it?
Is it waiting for the user to press a key?
May 13, 2013 at 1:22am UTC
It stops before printing "error" and highlights line, but doesn't tell me what's wrong with the command or what I need to change.
May 16, 2013 at 9:42am UTC
Daleth, thank you. Donnie
May 16, 2013 at 10:43am UTC
you're saying it doesn't output the area?
try to simplify stuff by doing this: area=(radius*radius)*3.14;
Aceix.