#include%26lt;stdio.h%26gt;
main(choice,r,s,side1,side2,a)
int choice;
float r,s,side1,side2,a;
{
do{
printf("\n");
printf("to calculate circle area -%26gt;1");
printf("\n");
if (choice==1)
{
printf("enter radius of circle\n");
scanf("%d",r);
printf("\n");
a=3.14*pow(r,2.);
printf("%d",a);
}
}
while(choice!=-1);
}
Hello. Why does not this simple C programme work( cannot exactly calculate area of a circle with input radius)
You are trying to print out a as an integer.
Reply:Ok, a couple of problems.
First of all, on line 2 it says main(choice,r,s,side1,side2,a) but that doesn't make sense. I'm guessing you're trying to do one of three things. 1) You're trying to make a function prototype. 2) You're trying to make a function definition 3) You're trying to call the function. At the moment, you're not doing any of those things and so the compiler is probably giving you an error about that.
Function prototypes tell the compiler "I'm going to have a function with this name, and these kind of arguements are passed to it, but I won't tell you how it works until later." Function prototypes are optional. If that's what you're trying to do, you need to specify what type of variables are getting passed (int, double, etc) to that function.
Function definitions tell the compiler "Ok, I have a function with these types of variables passed in, and I'll tell you how it works." You should have an open brace ( { ) after that line if you want to start the function definition.
To call a function, just use it like a printf() function, except use the name of the function. Don't forget the semicolon, and be sure to pass the right number and type of variables to it.
Next problem, take a look at your loop. You're doing a do-while loop, and it checks to see if choice does not equal -1.
However, you do a comparison check of choice in line 11. That means that you are using an uninitialized variable. When you declare a variable it grabs a random chunk of memory and says "This is your memory space for now", but you have no idea what was in that memory before. It could contain random numbers, you just don't know. You should assign a value to choice before using it for anything.
Hopefull that gets you a little closer to a working program. Feel free to ask if you have more questions.
Reply:try using the header file %26lt;math.h%26gt;
Cos, u've included 'pow' it can be described if u use the header file, #include%26lt;math.h%26gt;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment