Problems with Structure Lab. Assistance appreciated

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

const double PI = 3.14159265358979323846;

struct Cone
{
double height; // height of the cone
double radius; // radius of the base of the cone
}shape;

void input(double& height, double& radius);
void setUp(double height, double radius, Cone *conePtr);
double getVolume(Cone *conePtr);
void output(Cone *conePtr);

int main()
{
Cone *conePtr;
conePtr = &shape;

double height;
double radius;

conePtr = new Cone;

input(height, radius);
setUp(height, radius, conePtr);
output(conePtr);

delete [] conePtr;
return 0;

}

void input(double& height, double& radius)
{
cout << "Enter the height of the cone: " << endl;
cin >> height;
cout << "Enter the radius of the base of the cone: " << endl;
cin >> radius;
}

void setUp(double height, double radius, Cone *conePtr )
{

conePtr->height = height;
conePtr->radius = radius;
}

double getVolume(Cone *conePtr)
{
double Volume;
double height;
double radius;
conePtr->height = height;
conePtr->radius = radius;
Volume = PI * radius * radius * height / 3 ;
return Volume;
}

void output(Cone *conePtr)
{
double Volume;
getVolume(conePtr);
cout << "The height of the cone is " << conePtr->height << endl;
cout << "The radius of the base of the cone is: " << conePtr->radius << endl;
cout << "The volume of the cone is: " << Volume << endl;

}

I am having trouble with getting the right values out of the lab. I keep getting junk back when I run the program. This is what I get back when running the program

Enter the height of the cone:
6
Enter the radius of the base of the cone:
2
The height of the cone is 2.54505e-307
The radius of the base of the cone is: 9.61639e-317
The volume of the cone is: 2

Process returned 0 (0x0) execution time : 3.182 s
Press any key to continue.

I believe the problem is that the data isn't getting stored into the array and Idk how to fix that. I want the program to be able to get user input. Then take that input and store it into a structure. Then it has to compute the volume and display the output.
delete [] conePtr;
This line shouldn't have the square brackets. You didn't declare an array when you said conePtr = new Cone;
In your getVolume function, try changing these lines
1
2
conePtr->height = height;
conePtr->radius = radius;
to
1
2
height = conePtr->height;
radius = conePtr->radius;
The way it is right now you blow away what's stored inside conePtr->radius with whatever garbage is on the stack when double radius; is declared in the function. Same goes for height.
Last edited on
Thanks so much for the help. Deleting and switching around the syntax in the equation worked. Just one other question. I'm still getting the value of the volume to be 2 when I input 6 for the height and 2 for the radius but that's not correct. Is there something wrong with the equation or is it a logic error? It seems that whatever value I have the user enter for radius becomes the value for volume.

Last edited on
Topic archived. No new replies allowed.