Stupid question

Trying to practice with functions before moving on to references, but of course I can't get this program to work. The loops work fine, but my functions aren't connecting with "radius." I know it is a scoping issue, but if anyone can help that would be great.

// Function practice

#include <iostream>
#include <string>

using namespace std;

const double pi = 3.14159;

double circleCircumference(double radius);
double circleArea(double radius);
double sphereVolume(double radius);

int main()
{
cout << "Please choose which shape you would like to work with.\n";
string response1 = "nothing";

do
{
cout << "Please enter 'circle', 'square' or 'triangle':";
cin >> response1;

} while (response1 != "circle" && response1 != "square" && response1 != "triangle");


string response2 = "nothing";
do
{
cout << "Would you like to find the circumference, area, or volume of " << response1 << "? ";
cin >> response2;
} while (response2 != "circumference" && response2 != "area" && response2 != "volume");



if (response1 == "circle" && response2 == "circumference")
{
cout << "Please enter the radius: ";
double radius =0;
cin >> radius;
cout << "The circumference is " << circleCircumference(radius) << endl;
}

if (response1 == "circle" && response2 == "area")
{
cout << "Please enter the radius: ";
double radius = 0;
cin >> radius;
cout << "The area is " << circleArea(radius) <<endl;
}

if (response1 == "circle" && response2 == "volume")
{
cout << "Please enter the radius: ";
double radius = 0;
cin >> radius;
cout << "The volume of the sphere is " << sphereVolume(radius) <<endl;

return 0;
}

double circleCircumference (double radius)
{
double circumference = 2 * pi * radius;
return circumference;
}

double circleArea (double radius)
{
double area = pi * pi * radius;
return area;
}

double sphereVolume (double radius)
{
double volume = (pi^3 * 4 * radius)/3
return volume;
}

would it work if you do it like this?

1
2
double volume =  sphereVolume(radius);
cout << "The volume of the sphere is " << volume <<endl; 
I just tried that, and didn't seem to work. Unless I was supposed to put in the function itself.

I can't believe I'm having so much trouble with this.
Please always use code tags. Edit your post, select the code, press the <> button on the formatting menu on the right. It will format it properly and show line numbers.



double volume = (pi^3 * 4 * radius)/3

C++ does not have a ^ exponent operator - you must use the pow function in <cmath>. The formula is wrong as well.

The ^ operator is the bitwise eXclusive OR (XOR), which is entirely different.

http://www.cplusplus.com/doc/tutorial/operators/


Numbers like 2 * pi, (4.0 / 3.0) * pi etc are constants, so you can work out what they are and make them consts like you have with pi.

Your formula for circle area is wrong. Should be pi * r * r. Sphere volume is wrong as well

With this wrong formula:

1
2
double volume = (pi^3 * 4 * radius)/3
return volume; 


Can be done in 1 return statement like this, with constant I mentioned above:

1
2
3
double sphereVolume (double radius) {
    return  (4_3pi * pow(radius, 3.0) )
}


I hope this helps


Last edited on
The code doesn't compile. There's a missing closing brace for the last "if" in function main.

This line is incorrect both C++ it doesn't compile, and mathematically inaccurate too.

double volume = (pi^3 * 4 * radius)/3

Try this instead:
double volume = pi * 4.0 * radius*radius*radius/3.0;

(Similarly, the formula for area should be πr² rather than π²r)

Once it compiled, the program seems to work.
Also, I forgot to mention that you have this code 3 times - so shouldn't you get this info earlier (once) and send it to the functions, or make it a function it self?

1
2
3
cout << "Please enter the radius: ";
double radius =0;
cin >> radius; 


Always be on the look out for repetitive code - it usually means there is a better way.
Hey Guys,

Thanks so much for the help. I'm at work now, and I'll let you know how it goes as soon as I implement your suggestions.
Hey All,

I finished the program yesterday, and it ran great (my wife was "super" impressed).

Thanks for your help and suggestions.
Topic archived. No new replies allowed.