Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

where is the error in this program? it outputs constant values of vcone and scone for different values of r and h.

+1 vote
asked Jul 23, 2018 by Kenneth Otieno Ouko (180 points)
//calculate volume and surface area of a cone using c++ function programe
#include<iostream>
#include<math.h>
using namespace std;
double vcone(float r,float h);
double scone(float r,float h);
int main(){
float r,h;
cout<<"enter the values of r,and h:"<<endl;
cin>>r>>h;
cout<<"volume of a cone is:"<<vcone<<endl;
cout<<"surface area of a cone is:"<<scone<<endl;
return 0;
}
double vcone(float r,float h){
    const float pi=3.142;
    float volume;
    volume=1/3.0*pi*pow(r,2)*h;
    return volume;
}
double scone(float r,float h){
    const float pi=3.142;
    float surfacacearea;
    surfacacearea=pi*r*(r+sqrt(pow(h,2)+pow(r,2)));
    return surfacacearea;
}

2 Answers

0 votes
answered Jul 23, 2018 by anonymous
In main function, the lines should be:

cout<<"Volume of the cone is: "<<volume<<endl;

cout<<"Surface area of the cone is: "<<surfacearea<<endl;
commented Jul 24, 2018 by anonymous
Isn't volume and surfacearea local variables to their respective funcs. answer will be to pass r, h to vcone and scone.

cout<<"volume of a cone is:"<<vcone(r,h)<<endl;
cout<<"surface area of a cone is:"<<scone(r,h)<<endl;
0 votes
answered Jul 31, 2018 by G (140 points)

You need to pass the parameters r and h to the functions vcone and scone, respectively, i.e. replace 

cout<<"volume of a cone is:"<<vcone<<endl;
cout<<"surface area of a cone is:"<<scone<<endl;

with 

cout<<"volume of a cone is:"<<vcone(r,h)<<endl;
cout<<"surface area of a cone is:"<<scone(r,h)<<endl;

The functions need input arguments to work. As it is you are printing the values of the function pointers.

Hope that helps.

GG

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...