# Needs help with my C++ code calculating Volumes with functions.?

• 03-13-2010
gulfx01
Needs help with my C++ code calculating Volumes with functions.?
Code:

```You will write a C++ program that calculates the volume of spheres or cubes based on user input. You will write C++ functions to calculate the volumes, and you will call these functions from your main() program. First your program will ask the user to enter 's' if they want to calculate the volume of a sphere, 'c' if they want the volume of a cube, or 'q' if they want to quit. If they enter 's', the program will prompt the user to enter the radius of the sphere. Your program will pass this value to your sphereVolume() function, which will return the answer so you can then display to the user from main(). Similarly, if they enter 'c' the program will prompt the user to enter the height of the cube, then call your cubeVolume() function to find the volume. The function will return the answer, and in main(), you will display it to the user. After displaying the answer, your program should loop to the beginning, and continue like this until the user enters 'q' to quit. Remember, volume of a cube = height^3 volume of sphere = (4/3) * PI * r^3 My code is: # include <iostream> # include <cmath> using namespace std; int main () { double cubeVolume(double height); double h = pow(h,3); double sphereVolume(double radius); double PI = 3.14159; double r = pow(r,3); cout << "sphereVolume is (4.0 / 3.0) * PI * pow(h,3) "; } { question: cout << "Do you want to calculate the volume of a sphere or cube(s/c)? (q for quit) "; char i; cin >> i; double result; if (i=='s') { cout << "Enter the sphere's radius: "; double getradius; cin >> getradius; cout << "The sphere's volume is " << sphereVolume(getradius) <<".\n"; } if (i=='c') { cout << "Enter the cube's height: "; double getheight; cin >> getheight; cout << "The cube's volume is " << cubeVolume(getheight) << ".\n"; } if (i!='c' && i!='s' && i!='q') { cout << "Invalid answer. s, c, or q please.\n"; goto question; } if (i!='q') goto question; return 0; }}```
• 03-13-2010
nicoqwertyu
Code:

```// This is a function declaration and doesn't belong in main double cubeVolume(double height); // You're declaring a variable named h and then initializing it to the the power of 3 // What you're trying to do is: // double h = pow(height, 3); double h = pow(h,3); // You're declaring a variable named 'h' and then taking the // This is a function declaration and doesn't belong in main double sphereVolume(double radius); // This is a constant and should be declared as such (also not in main) // const double PI = 3.14159 double PI = 3.14159; // Same problem as above // double r = pow(radius, 3) double r = pow(r,3); // Your homework assignment wants you to get the value from a function and display it main // vol = sphereVolume(radius); // std::cout << vol << std::endl; cout << "sphereVolume is (4.0 / 3.0) * PI * pow(h,3) "; } // End of main { // Random brackets, this should be a loop inside of main question: // Don't use goto cout << "Do you want to calculate the volume of a sphere or cube(s/c)? (q for quit) "; char i; cin >> i; double result; // Get into the habit of initializing variables if (i=='s') // i is generally used as an iteration variable, and not a char. It doesn't matter, but you should try and use meaningful variable names { cout << "Enter the sphere's radius: "; double getradius; cin >> getradius; cout << "The sphere's volume is " << sphereVolume(getradius) <<".\n"; // use std::endl } if (i=='c') // Have you used switch statements in your class yet? { cout << "Enter the cube's height: "; double getheight; cin >> getheight; cout << "The cube's volume is " << cubeVolume(getheight) << ".\n"; } if (i!='c' && i!='s' && i!='q') { cout << "Invalid answer. s, c, or q please.\n"; goto question; } if (i!='q') goto question; // No! return 0; }}```
[/QUOTE]

Use code tags.

Work on it some and come back. Impress your instructor with your initiative by looking up switch statements.