revolutionary
revolutionary

Reputation: 13

C++ functions with pointers

What is the error? How to solve it? This code is to find the area of the circle using pointers and functions. The error I am facing is that the &answer cannot be converted into float.

#include <iostream>

using namespace std;

void area(float, float);

int main()
{
    float radius, answer = 0.0;
    cout << "Enter a radius:";  // Take the radius from the user.
    cin  >> radius;
    area(&radius, &answer);
    cout << "Area of circle is:" << answer;
    return 0;

}

void area(float *value, float *result)  // This is the function to calculate the area.
{
    *result = 3.142 * (*value) * (*value);
}

Upvotes: 0

Views: 109

Answers (1)

imperial-lord
imperial-lord

Reputation: 641

You can do one of the 2 things:

  • Change the prototype to void area(float*, float*);
  • Remove the prototype and move the function:
    void area(float *value, float *result)  // This is the function to calculate the area.
    {
        *result = 3.142 * (*value) * (*value);
    }
    

above the main() function. Either of these will work.

Upvotes: 2

Related Questions