简体   繁体   中英

C Program for calculating area & perimeter of a circle won't compile

Problem:

When I try to compile the program I get 3 error statements, 2 of them are relating to using the pow math function to try and square the radius variable. The last error statement states that i'm missing a semi-colon on line 11 but I don't see where. I apologize for these newbie questions, hope you can help thanks.

What I've tried:

I've tried just doing "radius * radius" to get radius^2 rather than using pow(radius, 2,0); and it works but I still get the "line 11 expected; " error. Also for my assignment i'm required to use the pow function so its a necessity that I get that working.

My Code

#include <stdio.h> //Including standard library

double calculatingFunction(diameter) {
    const double pi = 3.14159;      //Declaring pi as a constant
    double radius = diameter * 0.5;     //Calculating radius
    double circumference = 2 * pi * radius; //Calculating circumference
    double area = pi * (pow(radius, 2.0));  //Calculating area

    return (circumference, area);   //returning values of circumference of area
}

//Beggning of main function
int main(void) {

    //Prompting User for Input
    printf("Please enter a value for the diameter of a circle"); 

    double diameter; //Variable to store User's input
    scanf("%lf", &diameter); //Scanning for User's input

    //Final Output listing the calculated perimeter and area of circle
    printf("\nPerimeter of Circle: %lf Area of Circle: %lf", 
    calculatingFunction(diameter));


    return 0; //End of Program

}

Errors:

homework.c: In function ‘calculatingFunction’:
homework.c:8: warning: implicit declaration of function ‘pow’
homework.c:8: warning: incompatible implicit declaration of built-in 
function ‘pow’
homework.c:11: error: expected ‘;’ before ‘of’

Programs purpose:

I've left comments on my code so it should be pretty clear what output is expected. The program is basically taking a user's input for the diameter of a circle and calculating the area and perimeter from it.

Firstly the error on line 11 is the word area written without indicating that it is a comment. Secondly pow is defined inside <math.h> . Including this will most likely resolve the issue, otherwise see this SO post.

Lastly, the main problem is in the return statement. In C you have to create a struct to return multiple values, or use pointer arguments.

typedef struct {
    double circumference, area;
} data;

data calculatingFunction(double diameter) {
    ...
    data ret = {circumference, area};
    return ret; //returning values of circumference of area
}

Also declare diameter to double or else the compiler will usually assume that the parameter is int . double seems more appropriate for this parameter.

pow() is declared in <math.h> . Did you include this in your headers?

(Also, your comment on line 9-10 is running onto a second line, escaping the commented-out part, and causing a compiler error.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM