Simple C++ program that calculates the measures of a sphere I have another home work from school and would like to see if I am on the right path to learning C++.

We have to do the calculation of the measures of a sphere. I did it two ways:


Just calculating everything at the main
Using a function




#include "stdafx.h"
#include "iostream" 
#include &lt;cmath&gt;



double *calcSphereMesures(double r);

double *calcSphereMesures(double r)
{
const double PI = 3.1416;
double d, area, circumference, volume;
double results[4];

d = 2*r;
circumference = PI * d;
area = (4 * PI * pow(r,2));
volume = (4 * PI * pow(r,3))/3;

results[0] = area;
results[1] = circumference;
results[2] = volume;
results[3] = d;
return results;
}

int main()
{
double r;
std::cout &lt;&lt; "Please enter the radius of a circle: ";
std::cin &gt;&gt; r;
double *results = calcSphereMesures(r);

std::cout &lt;&lt; "\n**************************************" &lt;&lt; "\n"
&lt;&lt; "* Area and Circumference of A Sphere *" &lt;&lt; "\n"
&lt;&lt; "**************************************" &lt;&lt; "\n"
&lt;&lt; "\nRadius: " &lt;&lt; r &lt;&lt; "\n"
&lt;&lt; "Diameter: " &lt;&lt; results[3] &lt;&lt; "\n"
&lt;&lt; "Area: " &lt;&lt; results[0] &lt;&lt; "\n"
&lt;&lt; "Circumference: " &lt;&lt; results[1] &lt;&lt; "\n"
&lt;&lt; "Volume: " &lt;&lt; results[2];
return 0;
}


I tried to check all the tips that were given to me the last time. Is that still bad code? What can I improve here and remember for future exercises?
I see a number of things that may help you improve your code.

Prefer portable code

This code could be entirely portable if the #include "stdafx.h" were omitted.  It's Windows specific and doesn't really help here, even on Windows.

Understand header include paths

There is a subtle difference between #include "iostream" and #include &lt;iostream&gt;.  Although it's implementation defined, most compiler implementations is that the quotation mark form searches locally first (e.g. the current directory) and then searches system include directories if that fails.  The angle bracket form usually searches in system include directories.  See https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename for more.

Fix your formatting

The code is all jammed up on the left margin with no indenting.  I don't know if that's how you wrote it or if it's a cut-and-paste error when posting the question, but it really makes the program hard to read.  It makes no difference to the compiler, but you should strive for a good consistent style that includes visual clues for the human reader of your code.  

Fix the bug

The calcSphereMesures routine is returning the address of a variable that's local to that routine.  That's a bug.  If you need to return multiple values, either pass in a pointer to an appropriate structure to be filled in by the routine or allocate one using new and returning that.  I'd prefer the former, because it makes ownership of the structure a little cleaner (that is, the caller must both allocate and free the structure).  Note that although I'm using the word structure, here, that isn't necessarily a struct -- it could just as well be an array or some other data structure.

Consider using an object

The area, circumference, etc., could all be attributes of a Sphere class.  This would have the advantage that one could write sphere.diameter() instead of results[3] for example.

Use name spaces

When you #include &lt;cmath&gt;, it brings those functions into the std:: namespace and may also put the symbols into the global namespace.  You're counting on the latter which is not guaranteed by the standard.  You can fix that by using std::pow instead of just pow.

Use a better value of \$\pi\$

There's little point in asking for the precision of a double if you're going to approximate \$\pi\$ as 3.1416.

Understand the use of function declarations

A function declaration is required before the use of the function.  Your separate declaration and implementation of the calcSphereMesures() function would make sense if the declaration were in its own header file, but that's not the case here.  For that reason, I'd suggest eliminating the declaration -- keeping them synchronized is just another task that you can eliminate.

Check your spelling

The word "measure" is consistently misspelled within the code.  It would be nice to fix that.