0

I am a newbie in C++ and I am quite confused in programmer - defined functions. It shows these errors

40 [Error] cannot convert 'double' to 'double(double, double)' in assignment
40 [Error] assignment of function 'double total_area(double, double)'

I cannot enter total_area = cross_area + side_area; If I try to remove the double, it results to more errors I can't find any information in youtube or in google that seems helpful.

    // P33_2.cpp This program illustrates the local and global variables and call-by-value.
    // This program computes the side area and the cross section area of a cylinder
    #include<iostream>
    #include<cmath>
    using namespace std;
    const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
    double cross_area(double r); // Function prototype for function cross_area
    double side_area(double r, double h); // Function prototype for function Side_area
    double total_area(double cross_area, double side_area);
    int main(void)
    {
        double h, r; //variables local to the main function
        cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
        cin >> r >> h;
        cout << endl;
        cout << "Before I do any computation or call any function, I want to let you know that \n";
        cout << "you have entered r = " << r << " and h = " << h << "." << endl;
        cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
        cout << "in the second one I will convert h \n";
        cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
        cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
        cout << "The total surface area is "<< total_area << "inch-sqr \n \n";
        return 0;
    }
    double cross_area(double r)
    {
        //Cross secion area includes the disks at the bottom and the top
        r = r * 0.3937; // converting r to inch
        return 2*PI*pow(r,2);
    }
    double side_area(double r, double h)
    {
        double area; //variable local to Side_area function
        h = h * 0.3937; // converting h to inch
        area = 2*PI*r*h;
        return area;
    }
    double total_area(double cross_area, double side_area)`enter code here`
    {
        total_area = cross_area + side_area;
        return 0;
    }
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
Scezui
  • 21
  • 4
  • 1
    Don't forget [`M_PI`](https://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c) exists. There's no reason to use a crappy approximation. – tadman Nov 20 '20 at 06:06
  • `total_area = cross_area + side_area;` What do you think the type of `total_area` is? Can you see where it is declared? Probably want a `double` in front of that. Or just a `return` directly. – kaylum Nov 20 '20 at 06:07
  • `total_area` is a function. You must call it, you can't just print it. – tadman Nov 20 '20 at 06:07
  • I'm voting to close as typo: `total_area = cross_area + side_area; return 0;` should be `return cross_area + side_area;` – Lukas-T Nov 20 '20 at 06:07
  • @churill Also the `<< total_area <<` stuff. – tadman Nov 20 '20 at 06:08
  • @tadman right! OP clearly knows how to call functions and how to return values from them, so I doubt there is any real problem in understanding these concepts. – Lukas-T Nov 20 '20 at 06:09
  • 1
    @tadman, Thanks I'll take note of it – Scezui Nov 20 '20 at 06:14
  • I'm just experimenting things so these kind of suggestions are much appreciated – Scezui Nov 20 '20 at 06:14
  • And by the way the program is working now though I will still test it if there is any logical errors – Scezui Nov 20 '20 at 06:15
  • 2
    @tadman M_PI is a POSIX extension, not in ISO C++. Since C++20 there is `std::numbers::pi` . – M.M Nov 20 '20 at 06:21
  • @M.M It does tend to be pretty common, but that's even better! – tadman Nov 20 '20 at 06:22

1 Answers1

0

See improved working code below

in your code, you have not passed any argument to total_area function. How do you think you will calculate area without passing arguments.

cout << "The total surface area is "<< total_area << "inch-sqr \n \n";

    #include<iostream>
    #include<cmath>
    using namespace std;
    const double PI = 3.14159; // This variable is defined globally, known to all functions in this program as PI
    double cross_area(double r); // Function prototype for function cross_area
    double side_area(double r, double h); // Function prototype for function Side_area
    double total_area(double cross_area, double side_area);

    int main(void)
    {
        double h, r; //variables local to the main function
        cout << "Enter the radius and the height of the cylinder in Cm <Enter> ";
        cin >> r >> h;
        cout << endl;
        cout << "Before I do any computation or call any function, I want to let you know that \n";
        cout << "you have entered r = " << r << " and h = " << h << "." << endl;
        double dcross_area =  cross_area(r);
        double dside_area= side_area(r,h);
        cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;
        cout << "in the second one I will convert h \n";
        cout << "The cross section area of the cylinder is " << cross_area(r) << " inch-sqr endl\n";
        cout << "The side area of the cylinder is " << side_area(r,h) << " inch-sqr \n\n";
        cout << "The total surface area is "<< total_area(dcross_area, dside_area)  << "inch-sqr \n \n";
        return 0;
    }

    double cross_area(double r)
    {
        //Cross secion area includes the disks at the bottom and the top
        r = r * 0.3937; // converting r to inch
        return 2*PI*pow(r,2);
    }

    double side_area(double r, double h)
    {
        double area; //variable local to Side_area function
        h = h * 0.3937; // converting h to inch
        area = 2*PI*r*h;
        return area;
    }
    double total_area(double cross_area, double side_area)
   {
      return cross_area + side_area;
    
   }
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29