Volume of cone using structures and pointers

Im writing a program about calculating the volume of a cone using pointers and structures.

Here are the directions:
-----------------------------------------------------------------------------
The volume of a cone is given by the formula:
V = Π r2 h / 3

For the value of Π use:
const double PI = 3.14159265358979323846;

Declare a structure named: Cone
containing:

height a double, the height of the cone
radius a double, the radius of the base of the cone
Create the following functions:

main

Contains three variables:
A pointer named ptr which points to a Cone structure
A double named height
A double named radius
Uses new to obtain space for the data structure
Calls the the input, setUp, and output functions
Deletes the space that was obtained using new
input:

Takes the height of the cone and radius of the base as reference parameters
Reads the height and radius from the user
Has a return type of void
setUp:

Takes three parameters by value: height, radius, and a pointer to the Cone
Puts the data into the data structure
Has a return type of void
getVolume:

Takes one parameter by value: a pointer to the Cone
Computes the volume
Returns the volume
output:

Takes one parameter by value: a pointer to the Cone
Calls the getVolume function to get the volume
Prints the height, radius, and volume in a neat format
Has a return type of void
Put the main function first.
Use the function and variable names specified above.
Arrange the functions in the order listed above.

-----------------------------------------------------------------------------

I keep getting errors saying Variable has incomplete type 'void' with functions like input and other errors with setup.

I also need help with the code inside the setup function as I'm not sure how to write that code by passing it by value when its being passed by reference in other functions. How am i supposed to retrieve that info in setup?

Other issue is how to 'Use new to obtain space for the data structure' in main. There is a line about it but I'm not sure it is correct.

The last issue is what should I put inside the parentheses of the three functions when they are called in the main function?


Im not very familiar with how to paste my code using the C++ shell but here is the link
cpp.sh/7qgj4
Last edited on
Hi,

Try making the parameters in the function declarations and definitions match. Use some variable names for the parameters in the declarations.

You are leaking memory: every new has to have a corresponding delete

Inside some of the functions, you are using variables that are out of scope, so send them to the function as arguments, update your declarations & definitions so everything is consistent.

There is a tutorial, reference and articles on the top left of this page, have a look at the different topics you are using, like new for example:

http://www.cplusplus.com/reference/new/
http://www.cplusplus.com/doc/tutorial/functions/

Good Luck !!
- Your input prototype doesn't match your implementation.
1
2
3
4
5
6
7
8
9
10
11
//void input(&double, &double);
// fixed
void input(double&, double&);

void input(double &height, double &radius)
{
    cout << "Cone height: ";
    cin >> height;
    cout << "Cone radius: ";
    cin >> radius;
}



- When calling input() you didn't pass any parameters.
- Likewise with your other functions.
- http://www.cplusplus.com/doc/tutorial/functions/
1
2
3
//input();
// fixed
input(height, radius);



- As TheIdeasMan mentioned above, for every new you need to have a delete.
1
2
3
4
5
6
7
8
//Cone *myCone = new Cone();
// Adhering to the instructions.
Cone *ptr = new Cone();

// . . .

// fixed; end of main()
delete ptr;




I also need help with the code inside the setup function as I'm not sure how to write that code by passing it by value when its being passed by reference in other functions. How am i supposed to retrieve that info in setup?

- When passing parameters by reference like with input(height, radius) the passed parameters are aliases for the original variable. So whatever you do in the input function, acts upon the original variable.
- It's best explained with an example. http://cpp.sh/7d3q
- Because input(height, radius) modified your variables of height and radius in main(), you can just pass those as parameters.

- This function is invalid as the return type is void.
- As you are returning the volume, which you declared as a double, change the return type of the function to double.
- Where are radius and height declared in this function? Why aren't you using the passed parameter, ptr?
- The point of passing parameters is to be able to use them in the function. As Cone *ptr is a pointer, you need to use the -> operator to access the member variables. Example: http://cpp.sh/4pdy
1
2
3
4
5
6
7
8
9
10
11
void getVolume(Cone *ptr)
{
    double volume;
    // where are radius and height declared?
    // btw, you can just do for example, int x = 23 * 34 - 100
    // instead of splitting them into 2 lines
    volume = (PI * pow(radius, 2) * height)/3.0;
    
    // invalid, as the return type is void
    return volume;
}

Last edited on
Topic archived. No new replies allowed.